White space in a program

For discussions about programming, and for programming questions and advice


Moderator: Forum moderators

Post Reply
don570
Posts: 626
Joined: Sat Nov 21, 2020 4:43 pm
Has thanked: 5 times
Been thanked: 98 times

White space in a program

Post by don570 »

I noticed something interesting regarding white space....
The following code didn't work as I thought.

Code: Select all

A=$(basename "$FILE")
mkdir -p  ""$A".extracted"

I thought that ""$A".extracted" would be the smart thing to do to protect white space in a file's name.
I wanted a folder to be created based on the file name.
However what it did was create MULTIPLE folders !!!! if there was some white space in the file name.

So I changed to following and it worked properly.

Code: Select all

A=$(basename "$FILE")
mkdir -p  "$A".extracted

The entire script is here...
http://forum.puppylinux.com/viewtopic.php?t=5291

User avatar
misko_2083
Posts: 196
Joined: Wed Dec 09, 2020 11:59 pm
Has thanked: 10 times
Been thanked: 20 times

Re: White space in a program

Post by misko_2083 »

don570 wrote: Sat Feb 26, 2022 8:46 pm

I noticed something interesting regarding white space....
The following code didn't work as I thought.

Code: Select all

A=$(basename "$FILE")
mkdir -p  ""$A".extracted"

I thought that ""$A".extracted" would be the smart thing to do to protect white space in a file's name.
I wanted a folder to be created based on the file name.
However what it did was create MULTIPLE folders !!!! if there was some white space in the file name.

So I changed to following and it worked properly.

Code: Select all

A=$(basename "$FILE")
mkdir -p  "$A".extracted

The entire script is here...
http://forum.puppylinux.com/viewtopic.php?t=5291

This would work too:

Code: Select all

A=$(basename "$FILE")
mkdir -p  "$A.extracted"

Unless the filename starts with "-" character.
Then the command will interpret it as an option.

Code: Select all

basename "-filename.ext"
basename: invalid option -- 'f'
Try 'basename --help' for more information.

mkdir -p "-filename"
mkdir: invalid option -- 'f'
Try 'mkdir --help' for more information.

So it's a better to separate the parameters with --

Code: Select all

basename -- "-filename.ext"
-filename

mkdir -p -- "-filename"
echo *
-filename

Working with filenames is tricky. ;)

Do you want to exit the Circus? The Harsh Truth
https://www.youtube.com/watch?v=ZJwQicZHp_c

User avatar
user1234
Posts: 413
Joined: Sat Feb 26, 2022 5:48 am
Location: Somewhere on earth
Has thanked: 154 times
Been thanked: 87 times

Re: White space in a program

Post by user1234 »

Or you can use-

Code: Select all

mkdir -p  "\"$A\".extracted"

But the resultant directory will be "filename". extracted.

The reason your code didn't work was that that doing ""$A".extracted" would be equal to $A".extracted", since '''' is like opening and closing quotes ('' and then '').

PuppyLinux 🐾 gives new life to old computers ✨

don570
Posts: 626
Joined: Sat Nov 21, 2020 4:43 pm
Has thanked: 5 times
Been thanked: 98 times

Re: White space in a program

Post by don570 »

Back to my first post...

mkdir -p ""$A".extracted" results in multiple folders being created if there is white space in the filename.
That's my warning.
___________________________

Post Reply

Return to “Programming”