Sitemap
Ads

Ads

SE

Back quote in linux

2 min readFeb 14, 2023

Back qoute i.e. “`” (key above tab) is used as Command Substitution in linux. There is one more way of command substitution that is $(cmd), we will see this later.
We often want execute a command and store the output in a variable for such cases we use back quote .

-> curDate=`date`

-> echo $curDate
Tue Feb 14 12:12:55 IST 2023

In the above snippet, “date” which i command is used inside back quote or back tick and assigned to a variable. So this will assign the date to the variable curDate, which can be seen in the next line.

We can use backquote with other commands also .

-> vi `find . -iname temp.txt`
#it will open the file in vi editor

For next part we need to know few things-

Get Ads’s stories in your inbox

Join Medium for free to get updates from this writer.

On a Unix-like operating system, the first three file descriptors, by default, are — STDIN (standard input) ,STDOUT (standard output) and STDERR (standard error). If command is executed with out error the output will be in STDOUT if not it will be in STDERR.

For such cases where you are using back quote to evaluate something which can produce stderr, we need to capture error also. By default backquote only captures STDOUT to any variable.

-> curDate=`datee`
-bash: datee: command not found
-> echo $curDate

#blank

As we can see here datee is not a proper command so it shows command not found but that is not captured by the variable.

To capture the STDERR also in the variable we can use redirection(>), so we need to redirect STDERR to STDOUT, we can do that appending 2>&1 after the command as shown below ..

-> curDate=`datee 2>&1`
-> echo $curDate
-bash: datee: command not found

We can also redirect to a file and also append using “>>” .

-> curDate=`dateee 2>errorFile.txt`
#writes the STDERR to errorFile.txt removing all contents of that file
-> curDate=`datee 2>>errorFile.txt`
#appends the STDERR to errorFile.txt

--

--