How to access shell environment variables in Perl script

Question: I would like to access several environment variables in my Perl script. How can I access shell variables from a Perl program? In some cases you may want to access shell environment variables (e.g., $PATH, $PWD, $HOME, $LANG) in your Perl script. While you can pass any needed environment variables to your Perl script […]
Continue reading…

 

How to detect the type of virtualization from the command line

Question: I am trying to write a shell script in which I need to detect whether the Linux server on which the script is running is a virtual machine, a container or actual bare-metal hardware. This is because the script should perform different tasks depending on whether the underlying server is virtual or physical. Is […]
Continue reading…

 

How to compare two version numbers in a shell script

Question: I am writing a shell script in which I need to compare two version number strings (e.g., “1.2.30” and “1.3.0”) to determine which version is higher or lower than the other. Is there a way to compare two version number strings in a shell script? When you are writing a shell script, there are […]
Continue reading…

 

How to find which shell I am using on Linux

Question: I often change between different shells at the command line. Is there a quick and easy way to find out which shell I am currently in? Also how can I find out the version of the shell? Find out Which Shell You are In There are different ways to tell what shell you are […]
Continue reading…

 

How to check weather forecasts from the command line on Linux

Question: I often check local weather forecasts on the Linux desktop. However, is there an easy way to access weather forecast information in the terminal environment, where I don’t have access to desktop widgets or web browser? For Linux desktop users, there are many ways to access weather forecasts, e.g., using standalone weather apps, desktop […]
Continue reading…

 

How to change PATH environment variable on Linux

Question: When I try to run an executable, it complains “command not found.” The executable is actually located in /usr/local/bin. How can I add /usr/local/bin to my PATH variable, so that I can run the command without specify the path? In Linux, PATH environment variable stores a set of directories to search for an executable […]
Continue reading…

 

How to get the process ID (PID) of a shell script

Question: I want to know the process ID (PID) of the subshell under which my shell script is running. How can I find a PID in a bash shell script? When you execute a shell script, it will launch a process known as a subshell. As a child process of the main shell, a subshell […]
Continue reading…

 

How to detect and patch Shellshock vulnerability in bash​​

Question: I would like to know how to test whether or not my Linux server is vulnerable to bash Shellshock bug, and how to protect my Linux server against the Shellshock exploit. On September 24, 2014, a bash vulnerability nicknamed “Shellshock” (aka “Bashdoor” or “Bash bug”) was discovered by a security researcher named Stephane Chazelas. […]
Continue reading…

 

How to find Ubuntu version and codename in a shell script

Question: I am writing a shell script where I need to obtain the release version and release name (or codename) of Ubuntu system. How can I get Ubuntu version/release name inside a shell script? There are many cases where you need to run some batch processing jobs which can vary across different Ubuntu releases (e.g., […]
Continue reading…

 

How to use case statement in shell script

Question: I want to use switch/case statement (like in C/C++) in my shell script. Can you show me case examples written for a shell script? In a shell script, the “case” statement is often useful when you want to change the control flow of a script based on the value of a variable or expression. […]
Continue reading…

 

How to run all scripts in a directory on Linux

Question: I have a bunch of scripts in a directory. I want to automatically run all the scripts in the directory, regardless of how many these are. If you want to run all scripts or executable binaries in a particular directory, you can use a command line utility called run-parts. This tool can automatically discover […]
Continue reading…