Spaces behaviour in shell


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Spaces behaviour in shell
# 1  
Old 04-03-2009
Spaces behaviour in shell

Hello,

I am a bit puzzled by the way my shell treats spaces in filenames.

An example will be way clearer than any explanation I can make:
Code:
$ ls test\ file\ with\ spaces
test file with spaces
$ var="test\ file\ with\ spaces"
$ echo $var
test\ file\ with\ spaces
$ ls $var
ls: cannot access test\: No such file or directory
ls: cannot access file\: No such file or directory
ls: cannot access with\: No such file or directory
ls: cannot access spaces: No such file or directory

The problem is the same if I am using double quotes around the filename or when I nest a command displaying the filename.

I've been crawling on the internet for a long time, but I haven't been able to find any documentation on this particular issue.
# 2  
Old 04-03-2009
Quote the variable with double quotes:

Code:
var="test file with spaces"
echo "$var"

Regards
# 3  
Old 04-03-2009
No, I also tried that, but it didn't make it !

If the spaces are escaped with backslashes, AND you put the name between quotes, your shell is going to think the backslashes are part of the filename. The problem resides lower, that's why you need to use xargs, but once you know that, you can just do anything ! I actually used an input from a file instead of a variable (my command was actually something like xargs md5sum < filelist > sumlist).
# 4  
Old 04-03-2009
Maybe I'm missing something but this is an example of my output:

Code:
$ ls -l "a b c"
-rw-r--r-- 1 user user 908 2008-11-24 21:02 a b c
$ zz="a b c"
$ ls -l "$zz"
-rw-r--r-- 1 user user 908 2008-11-24 21:02 a b c
$

Regards
# 5  
Old 04-03-2009
Ah, sorry, I actually didn't try that because my filenames also had other special characters that I needed to protect (typically the single and double-quotes can be messy, with sed for example), but I'll remember that it works for simple cases !

Thanks !
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Korn shell behaviour in AIX

Hi, Consider the code snippet below: fun() { while read x do echo $x done < somefile_that_does_not_exists } fun echo I am here Korn shell on HPUX prints the message "I am here", while the behaviour is different on AIX korn shell. We do not get the message on AIX. Any... (5 Replies)
Discussion started by: 116@434
5 Replies

2. Shell Programming and Scripting

insert spaces between characters with pure shell

A file contains: abcdef I need : a b c d e f It's easy with sed sed 's/./& /g'but this is embedded linux that doesn't have sed/awk. The shell is unknown but it's bashlike. Parameter expansion works and seems promising and. A question mark seems to work as a wildcard, but there doesn't seem... (5 Replies)
Discussion started by: fubaya
5 Replies

3. Shell Programming and Scripting

Spaces in filenames located in variables in shell.

Greetings. I am trying to do a script that will do some file copying for me. Unfortunately I have spaces in the directory names (which I cannot change) and the result is someone hard to achieve in shell scripts. I have searched everywhere on the web but does not manage to find the answer to... (3 Replies)
Discussion started by: Mr.Glaurung
3 Replies

4. Shell Programming and Scripting

Problem with blank spaces in shell script

Hi, I did the following script: DIR=`pwd`;for i in `find . -name GESTION -type d`;do cd $i/..;setfacl -R -m g:directores:rwx, GESTION;echo $DIR'/'$i;cd $DIR;done This code do the following actions: 1. Starting inside a folder, it's searching for any folder called "GESTION" 2. Every time... (3 Replies)
Discussion started by: argie01
3 Replies

5. Shell Programming and Scripting

Not able to pass string with spaces in shell

I have to pass a sentence in a file, the specs are as: cat run | sed 's/SRT/'$8'/g' | sed 's/plength/68/g' | sed 's/stcol/'$5'/g' | sed 's/encol/'$6'/g' | sed 's/brdtype/'$1'/g' | sed 's/brdtxt/'$3'/g' | sed 's/demotxt/Total '$2'/g' | sed 's/bantxt/ban_'$7'/g' | sed 's/validcodes/'$4'/g' >... (15 Replies)
Discussion started by: patilrakesh1984
15 Replies

6. UNIX for Dummies Questions & Answers

shell: reconcile language and sort behaviour

Hi Don't know if this is a dummy question, but let's give it a try. I yesterday had a problem with undefined behaviour in the sort shell command (I'm using bash), leading to different sort orders without apparent reasons. I resolved this by typing export LC_ALL="C" export LC_COLLATE="C"... (5 Replies)
Discussion started by: jossojjos
5 Replies

7. Shell Programming and Scripting

Pass a string with spaces to a shell script

I'm a beginner and wasn't able to google my problem... I would like to pass a string with spaces to a shell script. my test_shell: #!/bin/sh -vx ####################################################### # generate_documentation (c) Ch.Affolter Nov. 2009 Version 1.0 #... (3 Replies)
Discussion started by: vivelafete
3 Replies

8. UNIX for Advanced & Expert Users

TRIM spaces in shell

am get a value like ' 15' in a variable what is the easiest method i can follow to strip 15 out (3 Replies)
Discussion started by: anumkoshy
3 Replies

9. Shell Programming and Scripting

Strip leading and trailing spaces only in a shell variable with embedded spaces

I am trying to strip all leading and trailing spaces of a shell variable using either awk or sed or any other utility, however unscuccessful and need your help. echo $SH_VAR | command_line Syntax. The SH_VAR contains embedded spaces which needs to be preserved. I need only for the leading and... (6 Replies)
Discussion started by: jerardfjay
6 Replies

10. Shell Programming and Scripting

any explanation for thsi shell script behaviour

hello whats the difference between excuting a shell script as a)sh myscript.sh b). ./myscript.sh i noticed that my shell script works fine when i run it as . ./myscript .sh but fails when i run it as sh myscript.sh could anybody explain why. the shell script is very simple ... (9 Replies)
Discussion started by: xiamin
9 Replies
Login or Register to Ask a Question