Skip to contentSkip to conversation

Stupid spaces

DISCUSSION calin 6 participants 12 messages Jul 19, 2004 – Jul 21, 2004

lore
Share & tools
lore

calin

author
#0 lore raw

Hello all.

I know the following question is a dumb one, but I've tried to make a script working and I'm stuck.

I'll try to isolate the problem as follows.

I have a script which at some moment during the execution, needs to cd. So I assumed that the following would work:

file_path='"/some/where/Test Folder/"'
echo "$file_path"
cd "$file_path"

But surprise: it didn't. Every time I try to run it it gives me something like:

$ ./cding.sh
"/some/where/Test Folder/"
./cding.sh: line 5: cd: "/some/where/Test Folder/": No such file or
directory
$

although the Test Folder exist, is in the right path (/some/where/Test Folder) and it's permissions are 777.

Any idea?

TIA

Calin Cosma

---------------------------------------------------------------
Incearca acum noul sistem de dating oferit de portalul acasa.ro

#1 lore raw

file_path='test foo'
cd "$file_path"

Quoted 43 lines · calin

Hello all.

I know the following question is a dumb one, but I've tried to make a script working and I'm stuck.

I'll try to isolate the problem as follows.

I have a script which at some moment during the execution, needs to cd. So I assumed that the following would work:

file_path='"/some/where/Test Folder/"'
echo "$file_path"
cd "$file_path"

But surprise: it didn't. Every time I try to run it it gives me something like:

$ ./cding.sh
"/some/where/Test Folder/"
./cding.sh: line 5: cd: "/some/where/Test Folder/": No such file or
directory
$

although the Test Folder exist, is in the right path (/some/where/Test Folder) and it's permissions are 777.

Any idea?

TIA

Calin Cosma

---------------------------------------------------------------
Incearca acum noul sistem de dating oferit de portalul acasa.ro

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

author
#2 lore raw

Worked just great. Thank you all.

Quoted 48 lines · Chris DiTrani

file_path='test foo'
cd "$file_path"

Quoted 36 lines · calin

Hello all.

I know the following question is a dumb one, but I've tried to make a script working and I'm stuck.

I'll try to isolate the problem as follows.

I have a script which at some moment during the execution, needs to cd. So I assumed that the following would work:

file_path='"/some/where/Test Folder/"'
echo "$file_path"
cd "$file_path"

But surprise: it didn't. Every time I try to run it it gives me something like:

$ ./cding.sh
"/some/where/Test Folder/"
./cding.sh: line 5: cd: "/some/where/Test Folder/": No such file or
directory
$

although the Test Folder exist, is in the right path (/some/where/Test Folder) and it's permissions are 777.

Any idea?

TIA

Calin Cosma

Quoted 7 lines

---------------------------------------------------------------
Incearca acum noul sistem de dating oferit de portalul acasa.ro

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

Quoted 10 lines

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

---------------------------------------------------------------
Incearca acum noul sistem de dating oferit de portalul acasa.ro

---------------------------------------------------------------
Incearca acum noul sistem de dating oferit de portalul acasa.ro

#3 lore raw
In reply to calin
Quoted 11 lines · calin

Hello all.

file_path='"/some/where/Test Folder/"'
echo "$file_path"
cd "$file_path"

But surprise: it didn't. Every time I try to run it it gives me something like:

Quoted 8 lines
$ ./cding.sh
"/some/where/Test Folder/"
./cding.sh: line 5: cd: "/some/where/Test Folder/": No such file or
directory
$

although the Test Folder exist, is in the right path (/some/where/Test Folder) and it's permissions are 777.

It's not lying to you. The directory "/some/where/Test Folder/" doesn't exist, but I bet /some/where/Test Folder/ does. Remove all ' and " from your script and watch it work.

Enjoy.

#4 lore raw
Quoted 6 lines · Scott Taylor
Quoted 4 lines · calin

Hello all.

file_path='"/some/where/Test Folder/"'

Oops, sorry, you do need quotes, but only " not '
file_path="/some/where/Test Folder/"

Quoted 20 lines
Quoted 7 lines

echo "$file_path"
cd "$file_path"

But surprise: it didn't. Every time I try to run it it gives me something like:

Quoted 8 lines
$ ./cding.sh
"/some/where/Test Folder/"
./cding.sh: line 5: cd: "/some/where/Test Folder/": No such file or
directory
$

although the Test Folder exist, is in the right path (/some/where/Test Folder) and it's permissions are 777.

It's not lying to you. The directory "/some/where/Test Folder/" doesn't exist, but I bet /some/where/Test Folder/ does. Remove all ' and " from your script and watch it work.

Quoted 7 lines

Enjoy.

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

--
Scott

#5 lore raw
In reply to calin

instead of the echo do an ls and see what it says.

Steve Cohen

Quoted 45 lines · calin

Hello all.

I know the following question is a dumb one, but I've tried to make a script working and I'm stuck.

I'll try to isolate the problem as follows.

I have a script which at some moment during the execution, needs to cd. So I assumed that the following would work:

file_path='"/some/where/Test Folder/"'
echo "$file_path"
cd "$file_path"

But surprise: it didn't. Every time I try to run it it gives me something like:

$ ./cding.sh
"/some/where/Test Folder/"
./cding.sh: line 5: cd: "/some/where/Test Folder/": No such file or
directory
$

although the Test Folder exist, is in the right path (/some/where/Test Folder) and it's permissions are 777.

Any idea?

TIA

Calin Cosma

---------------------------------------------------------------
Incearca acum noul sistem de dating oferit de portalul acasa.ro

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#6 lore raw

Why the quotes?

[root@ford /home]# file_path="/root"
[root@ford /home]# echo $file_path
/root
[root@ford /home]# cd $file_path
[root@ford /root]#

Remember, most of the script stuff you can test on the command line.

----- Original Message -----
From: "scohen" <[email protected]>
To: "calin" <[email protected]>
Cc: <[email protected]>
Sent: Tuesday, July 20, 2004 1:48 PM
Subject: Re: Stupid spaces

Quoted 48 lines

instead of the echo do an ls and see what it says.

Steve Cohen

Quoted 42 lines · calin

Hello all.

I know the following question is a dumb one, but I've tried to make a script working and I'm stuck.

I'll try to isolate the problem as follows.

I have a script which at some moment during the execution, needs to cd. So I assumed that the following would work:

file_path='"/some/where/Test Folder/"'
echo "$file_path"
cd "$file_path"

But surprise: it didn't. Every time I try to run it it gives me something like:

$ ./cding.sh
"/some/where/Test Folder/"
./cding.sh: line 5: cd: "/some/where/Test Folder/": No such file or
directory
$

although the Test Folder exist, is in the right path (/some/where/Test Folder) and it's permissions are 777.

Any idea?

TIA

Calin Cosma

---------------------------------------------------------------
Incearca acum noul sistem de dating oferit de portalul acasa.ro

-
To unsubscribe from this list: send the line "unsubscribe linux-admin"

in

Quoted 8 lines
Quoted 3 lines

the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#7 lore raw
In reply to calin

Also,

You are using way to many quotes. Just write this:

file_path="/some/where/Test Folder/"
echo $file_path
cd $file_path

You already quoted the path once. There is no need for the quotes around the variable and there is no need for the single and double quotes around the path.

Also, put in a pwd after the cd so you can see where you are as a test.

Steve Cohen

Quoted 45 lines · calin

Hello all.

I know the following question is a dumb one, but I've tried to make a script working and I'm stuck.

I'll try to isolate the problem as follows.

I have a script which at some moment during the execution, needs to cd. So I assumed that the following would work:

file_path='"/some/where/Test Folder/"'
echo "$file_path"
cd "$file_path"

But surprise: it didn't. Every time I try to run it it gives me something like:

$ ./cding.sh
"/some/where/Test Folder/"
./cding.sh: line 5: cd: "/some/where/Test Folder/": No such file or
directory
$

although the Test Folder exist, is in the right path (/some/where/Test Folder) and it's permissions are 777.

Any idea?

TIA

Calin Cosma

---------------------------------------------------------------
Incearca acum noul sistem de dating oferit de portalul acasa.ro

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#8 lore raw
Quoted 8 lines · scohen

Also,

You are using way to many quotes. Just write this:

file_path="/some/where/Test Folder/"
echo $file_path
cd $file_path

Did you actually try this? Doesn't work in my shell. I'm sure he tried this first and only got more complicated when it failed to work.

In your above example $file_path is an unquoted string that turns into two args, '/some/where/Test' and 'Folder/', when expanded and passed to cd.

CD

#9 lore raw

oooooh

The catch is the space ... didn't even notice

Need to retest :p
----- Original Message -----
From: "Chris DiTrani" <[email protected]>
To: "scohen" <[email protected]>
Cc: <[email protected]>
Sent: Tuesday, July 20, 2004 2:20 PM
Subject: Re: Stupid spaces

Quoted 23 lines · scohen
Quoted 8 lines

Also,

You are using way to many quotes. Just write this:

file_path="/some/where/Test Folder/"
echo $file_path
cd $file_path

Did you actually try this? Doesn't work in my shell. I'm sure he tried this first and only got more complicated when it failed to work.

In your above example $file_path is an unquoted string that turns into two args, '/some/where/Test' and 'Folder/', when expanded and passed to cd.

CD

-
To unsubscribe from this list: send the line "unsubscribe linux-admin" in the body of a message to [email protected]
More majordomo info at http://vger.kernel.org/majordomo-info.html

#10 lore raw
In reply to calin

Looking at this thread, quite a few people seem to have problems with shell quoting.

As a general rule, literal values should be contained within single quotes, i.e.:

file_path='/some/where/Test Folder/'

while variable references should almost always be contained within double quotes:

	cd "$file_path"

Within double quotes, variable expansions (e.g. $foo), command substitutions (e.g. foo or $(foo)), arithmetic substitutions (e.g. $[1+2] or $((1+2)) ) and similar (i.e. anything beginning with or $) are still evaluated, and the backslash character can be used to quote any of $ " \ or a newline.

Within single quotes, everything up to the next single quote is taken literally (including the backslash character). If you need to include a single quote, use '\'', e.g.:

	$ echo 'it'\''s'
	it's

[This parses as 'it' \' 's' (without the spaces), which the shell interprets is it ' s (again, without the spaces).]

Unquoted variable expansions will subsequently be split into words so, as has already been noted, the commands:

file_path='/some/where/Test Folder/'
cd $file_path

will result in the "cd" built-in command being called with two arguments, namely:

/some/where/Test
and:
Folder/

For this reason, variable references should usually be enclosed in double quotes; failure to do so is the main reason why scripts fail on filenames (or other arguments) which contain spaces.

If you have trouble with shell syntax issues generally, it may help to compile and install the following program:

	#include <stdio.h>

	int main(int argc, char **argv)
	{
		int i;
		for (i = 0; i < argc; i++)
			printf("argv[%2d] = '%s'\n", i, argv[i]);
		return 0;
	}

You can then use it to see exactly what the shell is doing with your command line before it gets to the program, e.g.:

	$ file_path='/some/where/Test Folder/'
	$ args $file_path
	argv[ 0] = '/usr/local/bin/args'
	argv[ 1] = '/some/where/Test'
	argv[ 2] = 'Folder/'
	$ args "$file_path"
	argv[ 0] = '/usr/local/bin/args'
	argv[ 1] = '/some/where/Test Folder/'

It may also help to read the bash(1) manpage. OK, so it's a bit more than a "page" (I get 60 pages for 1.x, 86 pages for 2.x) but, if you use the command line regularly, you could easily spend hundreds of hours per year using bash.

Signature · 1 line
Glynn Clements <[email protected]>
author
#11 lore raw

Again, thank you all for your fast support.

I solved the problem. I somehow missed the double quotes and left them in the wrong place.

So now work like this:

file_path='/some/where/Test Folder/'
echo "$file_path"
cd "$file_path"

Quoted 71 lines · Glynn Clements

Looking at this thread, quite a few people seem to have problems with shell quoting.

As a general rule, literal values should be contained within single quotes, i.e.:

file_path='/some/where/Test Folder/'

while variable references should almost always be contained within double quotes:

	cd "$file_path"

Within double quotes, variable expansions (e.g. $foo), command substitutions (e.g. foo or $(foo)), arithmetic substitutions (e.g. $[1+2] or $((1+2)) ) and similar (i.e. anything beginning with or $) are still evaluated, and the backslash character can be used to quote any of $ " \ or a newline.

Within single quotes, everything up to the next single quote is taken literally (including the backslash character). If you need to include a single quote, use '\'', e.g.:

	$ echo 'it'\''s'
	it's

[This parses as 'it' \' 's' (without the spaces), which the shell interprets is it ' s (again, without the spaces).]

Unquoted variable expansions will subsequently be split into words so, as has already been noted, the commands:

file_path='/some/where/Test Folder/'
cd $file_path

will result in the "cd" built-in command being called with two arguments, namely:

/some/where/Test
and:
Folder/

For this reason, variable references should usually be enclosed in double quotes; failure to do so is the main reason why scripts fail on filenames (or other arguments) which contain spaces.

If you have trouble with shell syntax issues generally, it may help to compile and install the following program:

	#include <stdio.h>

	int main(int argc, char **argv)
	{
		int i;
		for (i = 0; i < argc; i++)
			printf("argv[%2d] = '%s'\n", i, argv[i]);
		return 0;
	}

You can then use it to see exactly what the shell is doing with your command line before it gets to the program, e.g.:

	$ file_path='/some/where/Test Folder/'
	$ args $file_path
	argv[ 0] = '/usr/local/bin/args'
	argv[ 1] = '/some/where/Test'
	argv[ 2] = 'Folder/'
	$ args "$file_path"
	argv[ 0] = '/usr/local/bin/args'
	argv[ 1] = '/some/where/Test Folder/'
Quoted 4 lines

It may also help to read the bash(1) manpage. OK, so it's a bit more than a "page" (I get 60 pages for 1.x, 86 pages for 2.x) but, if you use the command line regularly, you could easily spend hundreds of hours per year using bash.

---------------------------------------------------------------
Incearca acum noul sistem de dating oferit de portalul acasa.ro