Linux allows the use of Shell expansions, useful for using the output of commands in variables. The result can be used for processing, usually in a shell script.
The symbols that allow shell expansion are:
The $ () and “ expansions do the same thing. However, it is recommended to use the $ () because it is more readable and less susceptible to errors when copying a script.
To use the expansion, simply place the desired command between the brackets or between the phrases.
Example of expanding command output:
#! /bin/bash
TODAY=$ (date “+%d/%m”)
ID=`ID -un`
echo “Good morning $ID, today is $TODAY”
When running this short script, it can be seen that the output of the date and id commands were directed to the TODAY and ID variables that could be used later in the script:
$. /expansion Good morning Uira, today is 12/01
To use command expansions, it is common to also use a variable to store the result of the execution of the command.
The $ {} expansion allows you to delimit the name of a variable, and even make substitutions.
Example:
$ bug="cat” $ echo $cat bug
If you want to use the variable content together with a text, it won’t work:
$ echo “the $animals went for a walk” they went for a walk
For this to work, you must use the variable name between $ {}:
$ echo “the $ {animals} s went for a walk” the cats went for a walk
It is also possible to make substitutions, using the common bar “/”, searching for one instance and replacing it with another:
$ echo “The $ {animal/cat/dog} barked” The dog barked
Shell expansions are powerful for storing the result of a command in a variable. This allows you to process the results of the commands intelligently to create procedures or process the data.
Learn much more about Linux in our online course. You can register here. If you already have an account, or want to create one, just log in or create your user here.
Share
The sudo command on Linux executes a given command as if it were another user.…
The ss command on Linux is extremely useful for investigating sockets, providing various information about…
Free Linux command shows the amount of total memory in use and available, as well…
The shell has structures for testing conditions and executing certain program sequences several times (loop),…
The /etc/fstab file stores the configuration of which devices should be mounted and what is…
The Netcat Command in Linux or nc is a utility used to do “almost anything”…
This website uses cookies.