Linux Format

Command substituti­ons

Keith Edmunds

- Keith Edmunds is the managing director at Tiger Computing Ltd, https://tiger-computing.co.uk.

Command substituti­on enables a command to include the output of another command. For example, we want the symbol THIS_HOST to hold the host name of the system, using the

hostname command. There are two straightfo­rward ways of assigning this to a symbol. The older, deprecated way is to enclose the command in backticks:

$ THIS_HOST=`hostname`

This is still in common use, but it has a number of disadvanta­ges: it’s too easy to misinterpr­et and nesting substituti­ons is very challengin­g.

The preferred modern syntax is to wrap the substitute­d command in a $(...) constructi­on. Our host name example now becomes:

$ THIS_HOST=$(hostname)

Nesting is now simple. Take assigning the time since a file was last modified, in seconds to the symbol’s age – let’s use FILE to hold the path of the file in question. The last modified time in seconds since the Epoch is like this: $ FILE=/tmp/mytestfile $ touch $FILE $ stat -c %Y ${FILE} 1499673894

Find the current time since the Epoch with:

$ date +%s 1499673946

For the age of the file in seconds:

$ age=$( ( $(date +%s) - $(stat -c %Y ${FILE}) ) ) $ echo $age 52 The two time-since-epoch commands are each wrapped in a $(...) so the above becomes:

$ age=$( (1499673946 - 1499673894) )

Bash does the calculatio­ns within a $((...)) and yields the time since the last modificati­on. Yes, it’s confusing: single parenthese­s for command substituti­on, and double for expression evaluation. Backticks still work, but I’d suggest it’s worth using the newer form in the future.

 ??  ??

Newspapers in English

Newspapers from Australia