Difference between revisions of "Command substitution"

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to navigation Jump to search
m (Text replacement - "<source lang="bash">" to "<syntaxhighlight lang="bash" >")
Line 4: Line 4:
  
 
Command substitution means nothing more but to run a shell command and store its output to a variable or display back using [[echo command]]. For example, display date and time:
 
Command substitution means nothing more but to run a shell command and store its output to a variable or display back using [[echo command]]. For example, display date and time:
<source lang="bash">echo "Today is $(date)"</source>
+
<syntaxhighlight lang="bash" >echo "Today is $(date)"</source>
 
OR
 
OR
<source lang="bash">echo "Computer name is $(hostname)"</source>
+
<syntaxhighlight lang="bash" >echo "Computer name is $(hostname)"</source>
 
==Syntax==
 
==Syntax==
 
You can use the grave accent (`) to perform a command substitution. The syntax is:
 
You can use the grave accent (`) to perform a command substitution. The syntax is:
Line 13: Line 13:
 
<pre>$(command-name)</pre>
 
<pre>$(command-name)</pre>
 
===Command substitution in an echo command===
 
===Command substitution in an echo command===
<source lang="bash">echo "Text $(command-name)"</source>
+
<syntaxhighlight lang="bash" >echo "Text $(command-name)"</source>
 
OR
 
OR
<source lang="bash"> echo -e "List of logged on users and what they are doing:\n $(w)"</source>
+
<syntaxhighlight lang="bash" > echo -e "List of logged on users and what they are doing:\n $(w)"</source>
 
Sample outputs:
 
Sample outputs:
 
<pre>List of logged on users and what they are doing:
 
<pre>List of logged on users and what they are doing:
Line 28: Line 28:
 
<pre>var=$(command-name)</pre>
 
<pre>var=$(command-name)</pre>
 
Store current date and time to a variable called NOW:
 
Store current date and time to a variable called NOW:
<source lang="bash">NOW=$(date)
+
<syntaxhighlight lang="bash" >NOW=$(date)
 
echo "$NOW"</source>
 
echo "$NOW"</source>
 
Store  system's host name to a variable called SERVERNAME:
 
Store  system's host name to a variable called SERVERNAME:
<source lang="bash">SERVERNAME=$(hostname)
+
<syntaxhighlight lang="bash" >SERVERNAME=$(hostname)
 
echo "Running command @ $SERVERNAME...."</source>
 
echo "Running command @ $SERVERNAME...."</source>
 
Store current working directory name to a variable called CWD:
 
Store current working directory name to a variable called CWD:
<source lang="bash">CWD=$(pwd)
+
<syntaxhighlight lang="bash" >CWD=$(pwd)
 
cd /path/some/where/else
 
cd /path/some/where/else
 
echo "Current dir $(pwd) and now going back to old dir .."
 
echo "Current dir $(pwd) and now going back to old dir .."
Line 41: Line 41:
 
===Command substitution and shell loops===
 
===Command substitution and shell loops===
 
Shell loop can use command substitution to get input:
 
Shell loop can use command substitution to get input:
<source lang="bash">for f in $(ls /etc/*.conf)
+
<syntaxhighlight lang="bash" >for f in $(ls /etc/*.conf)
 
do
 
do
 
   echo "$f"
 
   echo "$f"
 
done</source>
 
done</source>
 
However, a recommend syntax is as follows for file selections:
 
However, a recommend syntax is as follows for file selections:
<source lang="bash">for f in /etc/*.conf
+
<syntaxhighlight lang="bash" >for f in /etc/*.conf
 
do
 
do
 
   echo "$f"
 
   echo "$f"

Revision as of 22:49, 29 March 2016

← Continue statementHomeChapter 5 Challenges →

Command substitution means nothing more but to run a shell command and store its output to a variable or display back using echo command. For example, display date and time: <syntaxhighlight lang="bash" >echo "Today is $(date)"</source> OR <syntaxhighlight lang="bash" >echo "Computer name is $(hostname)"</source>

Syntax

You can use the grave accent (`) to perform a command substitution. The syntax is:

`command-name`

OR

$(command-name)

Command substitution in an echo command

<syntaxhighlight lang="bash" >echo "Text $(command-name)"</source> OR <syntaxhighlight lang="bash" > echo -e "List of logged on users and what they are doing:\n $(w)"</source> Sample outputs:

List of logged on users and what they are doing:
  09:49:06 up  4:09,  3 users,  load average: 0.34, 0.33, 0.28
USER     TTY      FROM              LOGIN@   IDLE   JCPU   PCPU WHAT
vivek    tty7     :0               05:40     ?     9:06m  0.09s /usr/bin/gnome-
vivek    pts/0    :0.0             07:02    0.00s  2:07m  0.13s bash
vivek    pts/2    :0.0             09:03   20:46m  0.04s  0.00s /bin/bash ./ssl

Command substitution and shell variables

You can store command output to a shell variable using the following syntax:

var=$(command-name)

Store current date and time to a variable called NOW: <syntaxhighlight lang="bash" >NOW=$(date) echo "$NOW"</source> Store system's host name to a variable called SERVERNAME: <syntaxhighlight lang="bash" >SERVERNAME=$(hostname) echo "Running command @ $SERVERNAME...."</source> Store current working directory name to a variable called CWD: <syntaxhighlight lang="bash" >CWD=$(pwd) cd /path/some/where/else echo "Current dir $(pwd) and now going back to old dir .." cd $CWD</source>

Command substitution and shell loops

Shell loop can use command substitution to get input: <syntaxhighlight lang="bash" >for f in $(ls /etc/*.conf) do

  echo "$f"

done</source> However, a recommend syntax is as follows for file selections: <syntaxhighlight lang="bash" >for f in /etc/*.conf do

  echo "$f"

done</source> ← Continue statementHomeChapter 5 Challenges →