Set whoami to variable
BASH command output to the variable
Different types of bash commands need to be run from the terminal based on the user’s requirements. 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.
variable = $ (command [option ...] argument1 arguments2 ...)
variable = $ (/ path / to / command)
variable = `command [option ...] argument1 arguments2 ...`
variable = `/ path / to / command`
*** Note: Don't use any space before and after the equal sign when using the above commands.
Single command output to a variable
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.
Example # 1:
bash `date` command is used to show the current date and time. The following script will store the output of `date` command into $current_date variable by using command substitution.
$ echo "Today is $ current_date"
Example # 2:
`pwd` command shows the path of the current working directory. The following script stores the output of `pwd` command into the variable, $ current_dir and the value of this variable is printed by using `echo` command.
$ echo "The current directory is: $ current_dir"
Command with option and argument
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.
Example # 3:
Bash `wc` command is used to count the total number of lines, words, and characters of any file. This command uses -c, -w and -l as option and filename as the argument to generate the output. Create a text file named fruits.txt with the following data to test the next script.
Run the following commands to count and store the total number of words in the fruits.txt file into a variable, $ count_words and print the value by using `echo` command.
$ echo "Total words in fruits.txt is $ count_words"
Example # 4:
`cut` is another bash command that uses option and argument to generate the output. Create a text file named weekday.txt with seven weekday names to run the next script.
Create a bash file named cmdsub1.sh with the following script. In this script, while loop is used to read the content of weekday.txt file line by line and read the first three characters of each line by using `cut` command. After cutting, the string value is stored in the variable $ day. Next, If the statement is used to check the value of $ day is ‘Sun ’ or not. The output will print ‘Sunday is the holiday‘When if the condition is true otherwise it will print the value of $ day.
filename = 'weekday.txt'
whileread line; do
day = `echo $ line | cut-c1-3`
if [$ day == "Sun"]
echo "Sunday is the holiday"
echo $ day
done <$ filename
Run the script.
$ bash cmdsub1.sh
Using command substitution in loop
You can store the output of command substitution into any loop variable which is shown in the next example.
Example # 5:
Create a file named cmdsub2.sh with the following code. Here, `ls -d * /`command is used to retrieve all directory list from the current directory. For loop is used here to read each directory from the output and store it in the variable $ dirname which is printed later.
fordirnamein $ (ls-d * /)
echo "$ dirname"
Run the script.
Using nested commands
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.
Nested command syntax:
Example # 6:
Two commands, `echo`and`who`are used in this example as the nested command. Here, `who`Command will execute first that print the user’s information of the currently logged in user. The output of the `who`command will execute by`echo`command and the output of`echo`will store into the variable $ var. Here, the output of `echo`command depends on the output of`who`command.
$ echo $ var
Using Command path
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.
Example # 7:
`whoami` command shows the username of the currently logged in user. By default, this command is stored in /usr /am / folder. Run the following script to run `whoami` command using path and store in the variable, $ output, and print the value of $ output.
$ echo $ output
Using Command Line argument
You can use the command line argument with the command as the argument in the command substitution.
Example # 8:
Create a bash file named cmdsub3.sh with the following script. `basename`command is used here to retrieve the filename from the 2nd command line argument and stored in the variable, $ filename. We know the 1st command line argument is the name of the executing script which is denoted by $ 0.
filename = `basename $ 1`
echo "The name of the file is $ filename."
Run the script with the following argument value.
Here, the basename of the path, Desktop / temp / hello.txt is ‘hello.txt’. So, the value of the $ filename want to be hello.txt.
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:
- How to use Lactacyd White Intim Wash
- 64th birthday card beats when I am
- Ingane zoma ft sfiso ncwane if jesus
- Karachi Airport in 1980 that was given
- Who lives in eaton hall chester
- What does depressive disorder mean
- Symulator dachowania cena zlata
- Wsparcie w starcite starachowice pogoda
- Polimeni Antonella Sapienza Howard
- Aquaphor modern 4000l like many small
- What brand tuxedo wears James Bond wear
- Foshan Guangdong Mobile Accessories Wholesale
- How do I grab a punch of Galvaknuckles
- Samne Tomar Chowder Pahar to take lyrics
- How to get to Kabirvad from Vadodara India
- Howard Heck Adam Driver full interview
- How to measure the ferrite core permeability equation
- What is Cytozyme Orchic used for?
- LR20 battery mah what does that mean
- How to make cow dung patties
- Nso cenomar how much
- Who is Amanda Berry Dating
- Best of Robi Chowdhury album sales
- How old is Smokie Norful's daughter Ashley
- How to install slp loudspeaker resonators
- Kenaf production in Ghana which language
- How to Freeze Cooked Lobster Meat
- What does tax deduction mean on page 60
- What is the function of kh2po4 pka
- Tinkering how to build a container
- How To Use Factor Balance Booster
- John Daly Howard Heck interviews
- Taqdeer according to islam how did life go
- How to read m & e drawing