Galactic Civilizations 3
Command substitution in shell script

Command substitution in shell script

Category: Programming.
Posted by on the 16/01/2014.
Last update on the 17/01/2014.


This tutorial explains how to use the mechanism of command substitution in your shell script.


This tutorial is intended for people under Linux (Debian, Ubuntu, ...). It does not require any prerequisites and can be can be realized with a simple text editor like « Notepad »..


The command substitution allows executing a command and saving the results in a variable.

Usage example

The syntax of the command substitution is the following: $(myCommand)
Here is a usage example of the command substitution feature in shell:

#display the content of the current folder in the standard output.
echo $(ls)

Here is another example:

#save the content of the current folder
#in the variable "myvar"

echo $myvar

Substitution of several commands

The command substitution mechanism can be used on several commands. To do that, you need to separate each command with a semicolon « ; ».

set $(pwd ; whoami)
echo $1: $2
# tutorielsenfolie: /home/tutorielsenfolie

Can also combine multiple command substitutions. Here is an example:

echo $( ls $(pswd)/testSubstitution)

Here, we consider that testSubstitution is a directory that is located in the home directory of the user.

NB: the variables can be used in a command substitution. Here is an example:

echo $( ls $maVar/testSubstitution)


When a command substitution is used with the internal command « set », the following error can occurs:

set $( ls -l .testSubstitution)

bash: set: -w: invalid option
set: usage: set [--abefhkmnptuvxBCHP] [-o option] [arg ...]

#output of the command ls -l
ls -l .testSubstitution
-rw-r--r-- 1 tutorielsenfolie users 1342 Avr 11 2013 .testSubstitution)

Here, we consider that testSubstitution is a directory that is located in the current directory.

The error comes from the fact that the substitution contains a hyphen character « - ». The command is interpreted as an option that leads to an error. To solve this issue, we need to double the hyphen:

set -- $(ls -l .testSubstitution)
echo $1

Command substitution and locale parameters

Under Linux, the localized data as the dates change according to the locale parameters.

It is possible to display the locale parameter in command line. To do that, you can use the command « locale »:


These are the parameters that defined the format of the date returned by the « date » command.

If your application used the value returned by the « date » command, it will crash if it uses other locale parameters.
To solve this issue we can use the command substitution to define temporally the local parameters to use for one command. To do that, execute the following command:

LC_ALL=C date

NB: here, we set the default parameters « C » to the « date » command.

Here is an example that shows how to display the « month » with the « date » command:

set $(LC_ALL=C date) ; echo $2

You like this tutorial ?
Like this website in order to promote it on facebook !

You like this tutorial ? Share it with your friends !
Partager sur Facebook Partager sur Twitter Partager sur Myspace Partager sur Stumbleupon Soumettre sur Reddit Partager sur Digg Ajouter à vos favoris Technorati Ajouter à vos favoris Live Ajouter à vos favoris Google Ajouter sur vos favoris Yahoo