- How To Assign Output of a Linux Command to a Variable
- Subscribe to RSS
- Subscribe to RSS
- Bash Scripting Tutorial - 2. Variables
- BASH command output to the variable
Subscribe to RSS
When the user runs any command from the terminal then it shows the output if no error exists otherwise it shows the error message. Sometimes, the output of the command needs to be stored in a variable for future use. Shell command substitution feature of bash can be used for this purpose. How you can store different types of shell commands into the variable using this feature is shown in this tutorial. Bash commands can be used without any option and argument for those commands where these parts are optional. The following two examples show the uses of simple command substitution. The option and argument are mandatory for some bash commands. The following examples show how you can store the output of the command with option and argument into a variable. This command uses -c, -w and -l as option and filename as the argument to generate the output. Create a text file named fruits. Run the following commands to count and store the total number of words in the fruits. Create a text file named weekday. Create a bash file named cmdsub1. In this script, while loop is used to read the content of weekday. You can store the output of command substitution into any loop variable which is shown in the next example. Create a file named cmdsub2. How you can use multiple commands using pipe is shown in the previous example. But you can use nested commands in command substitution where the output of the first command depends on the output of the second command and it works opposite of the pipe command. If you know the path of the command then you can run the command by specifying the command path when using command substitution. The following example shows the use of command path. You can use the command line argument with the command as the argument in the command substitution. Create a bash file named cmdsub3. Various uses of command substitutions are shown in this tutorial. If you need to work with multiple commands or depended commands and store the result temporary to do some other tasks later then you can use this feature in your script to get the output. More info in the video: About the author Fahmida Yesmin I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. Fahmida Yesmin I am a trainer of web programming courses. View all posts.
Subscribe to RSS
Bash Scripting Tutorial - 2. Variables
For those of you that have dabbled in programming before, you'll be quite familiar with variables. For those of you that haven't, think of a variable as a temporary store for a simple piece of information. These variables can be very useful for allowing us to manage and control the actions of our Bash Script. We'll go through a variety of different ways that variables have their data set and ways we can then use them. Variables are one of those things that are actually quite easy to use but are also quite easy to get yourself into trouble with if you don't properly understand how they work. As such there is a bit of reading in this section but if you take the time to go through and understand it you will be thankful you did later on when we start dabbling in more complex scripts. A variable is a temporary store for a piece of information. There are two actions we may perform for variables:. Variables may have their value set in a few different ways. The most common are to set the value directly and for its value to be set as the result of processing by a command or program. You will see examples of both below. Before Bash interprets or runs every line of our script it first checks to see if any variable names are present. For every variable it has identified, it replaces the variable name with its value. Then it runs that line of code and begins the process again on the next line. Here are a few quick points on syntax. They will be elaborated on and demonstrated as we go into more detail below. Command line arguments are commonly used and easy to work with so they are a good place to start. When we run a program on the command line you would be familiar with supplying arguments after it to control its behaviour. We can do similar with our bash scripts. These are automatically set by the system when we run our script so all we need to do is refer to them.