BASH quoting behavior


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting BASH quoting behavior
# 1  
Old 12-29-2008
BASH quoting behavior

The block below isn't a surprise:
Code:
$ ls
file1 file2 file3
$ x=*
$ echo $x
file1 file2 file3
$ echo '$x'
$x
$ echo "$x"
*
$

But I found this block a bit bewildering:
Code:
$ echo $x'
>'
*

$

I'm wondering why substitution wasn't performed on the $x, since it was unquoted (as far as I can tell). Thanks to the guru who can shed light on this one Smilie
# 2  
Old 12-29-2008
Am not sure.

Let me give it a try.

The quote at the end of the unquoted variable nullifies the effect - of expanding it, instead replacing with what it has been assigned to.

Hence the '*' and a newline ( as given in the input )

the same applies to the below

Code:
echo $x"
>"

*

# 3  
Old 12-30-2008
Quote:
Originally Posted by na5m
The block below isn't a surprise:
Code:
$ ls
file1 file2 file3
$ x=*
$ echo $x
file1 file2 file3
$ echo '$x'
$x
$ echo "$x"
*
$

But I found this block a bit bewildering:
Code:
$ echo $x'
>'
*

$

I'm wondering why substitution wasn't performed on the $x, since it was unquoted (as far as I can tell).
As matrixmadhan already pointed out, It's because you're trying to match *\n (filename expansion occurs after word splitting):

Code:
bash-2.03$ ls
file1  file2  file3
bash-2.03$ x=*
bash-2.03$ echo \$x "$x" $x
$x * file1 file2 file3
bash-2.03$ echo *'
> '
*

bash-2.03$ touch 'newline_at_the_end
> '
bash-2.03$ echo $x'
'
newline_at_the_end

ls -b
file1                file2                file3                newline_at_the_end\012

# 4  
Old 12-30-2008
Quote:
Originally Posted by radoulov
..., It's because you're trying to match *\n (filename expansion occurs after word splitting):
OK, then how about this:
Code:
echo "$x $x
> "
* *

There is no newline after the first asterisk. It seems like it should at least work.

------
Oops. Never mind. Do this:
Code:
echo $x "
> "
file1 file2 file3

In my example above, I had the quote in the wrong place.
We just have to put a space after the $x.

Last edited by KenJackson; 12-30-2008 at 12:39 PM.. Reason: update
# 5  
Old 12-30-2008
Quote:
Originally Posted by KenJackson
Code:
echo "$x $x
> "
* *

There is no newline after the first asterisk. It seems like it should at least work.

Wildcards are not expanded insides quotes.
# 6  
Old 12-30-2008
Quote:
Originally Posted by KenJackson
Oops. Never mind. Do this:
Code:
echo $x "
> "
file1 file2 file3

In my example above, I had the quote in the wrong place.
We just have to put a space after the $x.
As I already said, filename expansion occurs after word splitting,
so now you have n (one in your case) words (based on your current IFS value, space, tab and newline by default): the content of $x. Therefore filename expansion is attempted on the value of $x if the required conditions are met (i.e. its content contains special characters).

Last edited by radoulov; 12-30-2008 at 02:33 PM.. Reason: correction
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Different behavior between bash shell and bash script for cmd

So I'm trying to pass certain json elements as env vars and use them later on in a script. Sample json: JSON='{ "Element1": "file-123456", "Element2": "Name, of, company written in, a very weird way", "Element3": "path/to/some/file.txt", }' (part of the) script: for s... (5 Replies)
Discussion started by: da1
5 Replies

2. UNIX for Advanced & Expert Users

Bash : While Loop behavior

Good Morning I think there may be something I dont understand fully. The following code works well, but I dont like the set domen method. #!/bin/bash # domen="y" while do echo " M A I N - M E N U" echo "1. Contents of /etc/passwd" echo "2. List of users currently logged" echo... (10 Replies)
Discussion started by: popeye
10 Replies

3. Shell Programming and Scripting

Help with Bash quoting

I am trying to write a BASH script that will prompt a user to enter a number of days, then calculate the date. My problem is the date command uses single or double quotes. For Example.. date -d "7 days" Here is an example of some same code I am trying to work through. echo "when do you... (4 Replies)
Discussion started by: javajockey
4 Replies

4. Shell Programming and Scripting

Tricky BASH quoting question

I have some data files that I can identify by a certain pattern in the names using find. Every one of those data files has an XML file associated with it (can be multiple data files per XML file). The XML file is always up one directory from the data file(s) in a folder calledRun##### -... (12 Replies)
Discussion started by: Michael Stora
12 Replies

5. UNIX for Advanced & Expert Users

unexpected behavior bash, set -o vi, history -a, and HISTFILE

I am trying to get my history in sync in multiple bash sections and things aren't working the way I expect. Desired behavior, hitting esc-K in all bash sessions (same userid and machine) will use the same history. Observed behavior: Esc-k shows the history of the current session, rather than... (8 Replies)
Discussion started by: gg48gg
8 Replies

6. UNIX for Advanced & Expert Users

Different redirection behavior in BASH/Linux when run under cron vice login ???

run_xfs_fsr is a xfs filesystem maintenance script designed to run under cron. The system is a home theater personal computer running mythbuntu 10.10, and is accessed remotely for these tests. cron runs a script, (xfs_fsr.sh) at 02:30 that runs the subject script under BASH and sets the... (3 Replies)
Discussion started by: keepitsimpleeng
3 Replies

7. Red Hat

BASH command not found strang behavior

Hi all I am relatively new to linux (specifically red hat). I have installed Fedora 13 on my machine and started playing with the terminal when i found a very strange behavior when typing a command that is not found: the terminal does not prompt me back. In other words, i am logged as root (or... (4 Replies)
Discussion started by: abohmeed
4 Replies

8. Shell Programming and Scripting

bash: correct quoting with find and exiv2?

Hi, I need to embed a metatag to image files which contain qrcodes, i usually do this with exiv -M "set Exif.Image.DocumentName `zbarimg -q -Sdisable -Sqrcode.enable --raw image.tif`" image.tif which works fine. However I need to do this recursivly for whole directory and subdiretory... (4 Replies)
Discussion started by: mcframe
4 Replies

9. Shell Programming and Scripting

Quoting issue: Trouble with bash strings in script

I can do this on the command line: sqsh -S 192.168.x.x -o tmp -U user -P fakepass -D horizon -C "\ select second_id from borrower where btype like '%wsd%' " I can also just leave the SQL at the end intact on one line .... ... However, when I throw this in a script like: $SQSH -o... (4 Replies)
Discussion started by: Bubnoff
4 Replies

10. Shell Programming and Scripting

Mimic bash history behavior

Does anyone know of a way to mimic the up arrow/down arrow type bash behavior within a shell script? Say I have a scripted menu, and would like to be able to up arrow to bring up the last X number of lines of user input? Thanks to anybody with a suggestion. :) (0 Replies)
Discussion started by: sysera
0 Replies
Login or Register to Ask a Question