Cat and variables


 
Thread Tools Search this Thread
Top Forums UNIX for Dummies Questions & Answers Cat and variables
# 8  
Old 09-04-2014
A \ in a file name is unusual. (Unless Microsoft will change their directory delimiter to / and let Explorer propose "New\File").
On the other hand, there are some tools that present a space as \ ; even some shells do that.
# 9  
Old 09-05-2014
Quote:
Originally Posted by MadeInGermany
On the other hand, there are some tools that present a space as \ ; even some shells do that.
The explanation for this is easy:

The shell uses space characters as field delimiters. A script called in the form

Code:
script.sh arg1 arg2

will interpret "arg1" as the first argument and "arg2" as the second. The reason is that the shell splits its input line into three "words", interprets the first as a command, then feeds this the second and third as arguments 1 and 2.

If you want to include such a separator character into your regular argument you have to somehow tell the shell to ignore its special function and treat it as a normal character. There are two ways to do so: quoting and escaping.

Quoting means to enclose text parts into double or single quotes. Every special character loses its special meaning inside such a quoted string. Double quotes leave some of these special characters in place (like "*"), single quotes remove the special meaning from all characters. Btw.: the single and double quote characters also lose their special meaning - to introduce or close a quoted string - inside a quoted string:

Code:
"bla ... 'single quoted' ... bla"

is NOT a single quoted string inside a double-quoted string, but a double-quoted string, containing some single quotes. The same the other way round.

This makes it possible to send the script above one single argument containing a space char. Here it is:

Code:
script.sh "arg1 arg2"

This will call the script "script.sh" and feed it one argument, "arg1 arg2", because the shell ignores the field-separating power of the space char inside the quoted string. Use single instead of double quotes and the effect is the same, because both sorts of quoting have the same effect on space chars.

Save for quoting there is one other option: escaping. Escaping means to prepend a character with a backslash ("\") which works like (single-) quoting, but only for the character immediately following. The following are equal:

Code:
script.sh "arg1" arg2
script.sh 'arg1' arg2
script.sh \a\r\g\1 arg2

Will work the same way like example 1, because the escaped characters "a", "r", "g" and "1" have no special powers which could be stripped from them, neither by quoting nor by escaping.

You probably know by now how to combine the two arguments into one: escape the space:

Code:
script.sh arg1\ arg2

This is why some shells include this automatically when they do TAB-completion. A "cd"-command int a directory "/foo/bar baz/" will work that way:

Code:
cd /foo/bar\ baz      # will work
cd "/foo/bar baz"     # will work too
cd /foo/bar baz       # would not work - "baz" would not be recognized as
                      # part of the command


Your error was to "overdo" it: putting a variable into double-quotes already "escapes" its whole content, therefore the - additionally escaping "\" was taken to be meantt literally. Either use the backslash and remove the quotes or (the better solution) let the quotes in place but remove the escaping char.

I hope this helps.

bakunin
 
Login or Register to Ask a Question

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

How to pass variables into anothother variables?

Below are three variables, which I want to pass into variable RESULT1 username1=userid poihostname1=dellsys.com port1=8080 How can I pass these variables into below code... RESULT1=$((ssh -n username1@poihostname1 time /usr/sfw/bin/wget --user=sam --password=123 -O /dev/null -q... (4 Replies)
Discussion started by: manohar2013
4 Replies

2. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

4. Shell Programming and Scripting

Display variables in CAT area

Hi All, I've got a script to output YAML data, and I want to display data that's held inside variables inside one large CAT area. What's the easiest way to do this? cat << "END" --- classes: - general_image - $intro #Variable 1 - $mid #Variable 2 ... (2 Replies)
Discussion started by: glarizza
2 Replies

5. Shell Programming and Scripting

cat in the command line doesn't match cat in the script

Hello, So I sorted my file as I was supposed to: sort -n -r -k 2 -k 1 file1 | uniq > file2 and when I wrote > cat file2 in the command line, I got what I was expecting, but in the script itself ... sort -n -r -k 2 -k 1 averages | uniq > temp cat file2 It wrote a whole... (21 Replies)
Discussion started by: shira
21 Replies

6. Shell Programming and Scripting

for i in `cat myname.txt` && for y in `cat yourname.txt`

cat myname.txt John Doe I John Doe II John Doe III ----------------------------------------------------------------------- for i in `cat myname.txt` do echo This is my name: $i >> thi.is.my.name.txt done ----------------------------------------------------------------------- cat... (1 Reply)
Discussion started by: danimad
1 Replies

7. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

8. UNIX for Dummies Questions & Answers

Difference between cat , cat > , cat >> and touch !!!

Hi Can anybody tell the difference between Difference between cat , cat > , cat >> and touch command in UNIX? Thanks (6 Replies)
Discussion started by: skyineyes
6 Replies

9. Shell Programming and Scripting

cat setting variables

hi All I have a file that has 4 lines: 1. yesterday's date (mm/dd/yyyy) 2. yesterday's day- dd 3. yesterday's month- mm 4. yesterday's year- yyyy I want to read this file and place them in variables. how can I do this. Please help. thanks in advance!! KS (3 Replies)
Discussion started by: skotapal
3 Replies
Login or Register to Ask a Question