ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
The backticks (`) force the output of a program/shell call through (or into) it's caller. Your example above:
Quote:
cat `ls -al ~/ | grep Desktop`
basically forces the output of the command "ls -al ~/ | grep Desktop" into the cat program, which displays it. Obviously, this is example is absurd because "cat" only dumps it to the display, which is exactly what "ls" does, so the "cat" and the backticks are useless in this example and will only slow down the script.
A very useful example of using backticks would be involving the "which" program. If you aren't familiar with it, "which" allows you to locate and display the full path of any binary/executable in the PATH. So, if you wanted to know the exact location of the binary "bunzip2" was located, you could do the following:
Quote:
~> which bunzip2
/bin/bunzip2
Well, let's say that you're writing a script which uses bunzip2, and you want to make sure that it exists on the system before the script starts tearing things up. Here's an example of how it could start:
Code:
#!/bin/bash
# SuperUnbackup.sh - use bunzip2 to decompress archives that exist
# in the /tmp directory
# Use backticks to force output of "which bunzip2" into a variable
#
Vbunzip2=`which bunzip2`
# Now, check to see that it's not empty
#
if [ "x$Vbunzip2" = "x" ]; then
echo "Could not find bunzip2. It must be installed."
exit
fi
# now find all bz2 files in the /tmp dir and uncompress them
# into the current directory
#
for file in /tmp/*.bz2; do
$Vbunzip2 -v $file
done
exit
This type of use of backticks is very common.
Now, as for single quotes ('), they are related to double quotes ("). Quotes are used to group CL items together. A good example of this would be to demonstrate how quotes work when passing items on the command line.
Suppose that we have a script called pass.sh that does nothing more than print out command line arguments and shows their positions:
We'll first put several items on the command line w/o using quotes, and another with quotes, to show how the grouping works:
Quote:
~> ./pass.sh this is a test of command line passing 1: this
2: is
3: a
4: test
5: of
6: command
~> ./pass.sh 'this is a' test of 'command line passing' 1: this is a
2: test
3: of
4: command line passing
5:
6:
The single (') and double (") can sometimes be used interchangeably, however, they are different. The singles (') preserve what the doubles (") expand. I'll show you an example by first using " to display the PATH variable, and then what happened when you use single quotes instead:
Notice how the single quotes did not expand the $PATH variable, but the doubles did. The expansion of a variable like this is called "interpolation". The single quotes don't interpolate variables.
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789
Rep:
Quote:
Your example above:
Quote:
cat `ls -al ~/ | grep Desktop`
basically forces the output of the command "ls -al ~/ | grep Desktop" into the cat program, which displays it. Obviously, this is example is absurd because "cat" only dumps it to the display, which is exactly what "ls" does, so the "cat" and the backticks are useless in this example and will only slow down the script.
Hmm, this analysis is wrong, "cat" is not dumping the file names to the display (as "ls" do), but the file's content, which is quite different.
The backticks (`) force the output of a program/shell call through (or into) it's caller. Your example above:
basically forces the output of the command "ls -al ~/ | grep Desktop" into the cat program, which displays it. Obviously, this is example is absurd because "cat" only dumps it to the display, which is exactly what "ls" does, so the "cat" and the backticks are useless in this example and will only slow down the script.
A very useful example of using backticks would be involving the "which" program. If you aren't familiar with it, "which" allows you to locate and display the full path of any binary/executable in the PATH. So, if you wanted to know the exact location of the binary "bunzip2" was located, you could do the following:
Well, let's say that you're writing a script which uses bunzip2, and you want to make sure that it exists on the system before the script starts tearing things up. Here's an example of how it could start:
Code:
#!/bin/bash
# SuperUnbackup.sh - use bunzip2 to decompress archives that exist
# in the /tmp directory
# Use backticks to force output of "which bunzip2" into a variable
#
Vbunzip2=`which bunzip2`
# Now, check to see that it's not empty
#
if [ "x$Vbunzip2" = "x" ]; then
echo "Could not find bunzip2. It must be installed."
exit
fi
# now find all bz2 files in the /tmp dir and uncompress them
# into the current directory
#
for file in /tmp/*.bz2; do
$Vbunzip2 -v $file
done
exit
This type of use of backticks is very common.
Now, as for single quotes ('), they are related to double quotes ("). Quotes are used to group CL items together. A good example of this would be to demonstrate how quotes work when passing items on the command line.
Suppose that we have a script called pass.sh that does nothing more than print out command line arguments and shows their positions:
We'll first put several items on the command line w/o using quotes, and another with quotes, to show how the grouping works:
The single (') and double (") can sometimes be used interchangeably, however, they are different. The singles (') preserve what the doubles (") expand. I'll show you an example by first using " to display the PATH variable, and then what happened when you use single quotes instead:
Notice how the single quotes did not expand the $PATH variable, but the doubles did. The expansion of a variable like this is called "interpolation". The single quotes don't interpolate variables.
I hope that all of this helps!
I have a big book about bash and nothing was even as close to as informative about single back and double quotes
as yours is
that is a very thorough and appreciated Lesson.
I have a big book about bash and nothing was even as close to as informative about single back and double quotes as yours is that is a very thorough and appreciated Lesson.
I'm glad to help, even with my mistake which jlliagre pointed out. (=
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.