LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 01-27-2006, 03:02 AM   #1
carl0ski
Member
 
Registered: Sep 2004
Location: Melbourne, Victoria Australia
Distribution: Support those that support you :)
Posts: 872
Blog Entries: 12

Rep: Reputation: 30
i'm having torubles judging `' in bash.


any one have an ananlogy or hint to remember which is which and does what.

` below ~ seems to link a string of command to appear as one.

cat `ls ~/| grep Desktop`

will show contents of /home/user



but where is ' sposed to be used?
it seems to occur all over the place.

Last edited by carl0ski; 01-27-2006 at 03:13 AM.
 
Old 01-27-2006, 03:07 AM   #2
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
cat `ls ~/; grep Desktop`
This is bogus, perhaps is it the following ?
Code:
cat `ls ~/ | grep Desktop`
 
Old 01-27-2006, 03:13 AM   #3
carl0ski
Member
 
Registered: Sep 2004
Location: Melbourne, Victoria Australia
Distribution: Support those that support you :)
Posts: 872

Original Poster
Blog Entries: 12

Rep: Reputation: 30
Quote:
Originally Posted by jlliagre
This is bogus, perhaps is it the following ?
Code:
cat `ls ~/ | grep Desktop`
lol yea it is a typo there

but that isnt the issue i error judging ' ` usage
 
Old 01-27-2006, 07:35 AM   #4
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
Quote:
but that isnt the issue i error judging ' ` usage
back-quote means: replace this by the output of the commands enclosed.

By the way, it is a poor and obsolete syntax, one should use instead $(command).
 
Old 01-27-2006, 09:00 AM   #5
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
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:
Code:
#!/bin/bash

counter=1

echo "1: $1"
echo "2: $2"
echo "3: $3"
echo "4: $4"
echo "5: $5"
echo "6: $6"
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:

Quote:
~> echo "$PATH"
/bin:/usr/bin:/usr/local/bin:/opt/www/htdig/bin
~> echo '$PATH'
$PATH
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!
 
Old 01-27-2006, 10:01 AM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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.
 
Old 01-27-2006, 10:58 AM   #7
carl0ski
Member
 
Registered: Sep 2004
Location: Melbourne, Victoria Australia
Distribution: Support those that support you :)
Posts: 872

Original Poster
Blog Entries: 12

Rep: Reputation: 30
Quote:
Originally Posted by jlliagre
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.
i dont know but the basic idea of the example was

cat `ls ~/`|grep Desktop
and
cat `ls ~/|grep Desktop `

have completely different outputs
 
Old 01-27-2006, 11:00 AM   #8
carl0ski
Member
 
Registered: Sep 2004
Location: Melbourne, Victoria Australia
Distribution: Support those that support you :)
Posts: 872

Original Poster
Blog Entries: 12

Rep: Reputation: 30
Quote:
Originally Posted by TheLinuxDuck
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:
Code:
#!/bin/bash

counter=1

echo "1: $1"
echo "2: $2"
echo "3: $3"
echo "4: $4"
echo "5: $5"
echo "6: $6"
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.

it just skims and says but a quote here. :S
 
Old 01-27-2006, 11:36 AM   #9
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Quote:
Originally Posted by jlliagre
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.
You are correct, I apologize.
 
Old 01-27-2006, 11:39 AM   #10
TheLinuxDuck
Member
 
Registered: Sep 2002
Location: Tulsa, OK
Distribution: Slack, baby!
Posts: 349

Rep: Reputation: 33
Quote:
Originally Posted by carl0ski
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. (=
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash: Print usage statement & exit; otherwise continue using Bash shorthand operators stefanlasiewski Programming 9 02-07-2006 05:20 PM
$LINENO can't be modified in bash 3.0, while it can be in bash 2.05b Darwish Linux - Software 1 11-07-2005 02:57 PM
Judging readiness for LPI 101 exam? Pcghost Linux - Certification 5 08-07-2005 10:30 PM
why did bash 2.05b install delete /bin/bash & "/bin/sh -> bash"? johnpipe Linux - Software 2 06-06-2004 06:42 PM
judging by uid£½0£¿ or e_uid=0? Anniebaby Linux - Security 0 02-25-2004 08:58 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:40 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration