Score:0

Can't escape spaces in variable name

cn flag

I trying to deal with spaces in directory names. As an example:

drive=/mnt/g; export drive
mydir="$drive/directory with spaces in name"
echo "$mydir"

The result:

/mnt/g/directory with spaces in name

Which of course, blows up on use, as bash sees this as separate arguments. The desired result would be something like:

"/mnt/g/directory with spaces in name"

as a single argument for subsequent processing (moves, copies, or whatever). I had thought that a double quote does the trick. Obviously not. Bash keeps eating the double quotes. Can someone point me to the proper procedure for handling variables with embedded spaces?

I'm using the Windows subsystem for Linux in Windows 10, but Ubuntu 20.04 LTS has the same behavior, so I'm assuming what works for one, will work for both. (Something with ILS maybe, IDK.)

uz flag
Jos
The `$drive` variable is OK as it is. It's the subsequent command that needs work, e.g. `mv $drive/myfile /backup/` should become `mv "$drive/myfile" /backup/`.
hr flag
bash expands `"$mydir"` as a *single* argument - if it "blows up on use" then that's a because of something you're doing after (which you haven't shown)
Score:1
cn flag

Your code is fine! You have spaces in variable that why will be spaces out of echo as well. So if you want to use a parameter that contains spaces you have to put between quotes like you do.

example:

command "${variable1}" "${variable2}"
Score:0
ru flag

If the desired output is

"/mnt/g/directory with spaces in name"

the code you need is

drive=/mnt/g; export drive
mydir="\"$drive/directory with spaces in name\""
echo "$mydir"
bac0n avatar
cn flag
if you want to keep the quoting you normally use bash *operator* `${mydir@Q}`
kris44dad avatar
cn flag
Here's the part I don't get. A subsequent 'cd' command (for example) to that directory says, too many arguments. Bash is parsing on the spaces. This is happening in all the examples listed here. mydir="\"$drive/directory with spaces in name\"" gives me the variable with double quotes but still parses on the spaces when used. ${mydir@Q} gives me single quotes, and "${mydir}" gives me the same result as the original posting. I can't seem to pass a single argument to the shell. They are all passing multiple arguments when used.
ru flag
I haven't understood what you want to do. Do you want to `cd` to `$mydir` from within the bash script or do you want to create an environment variable in order to issue a `cd envVariable` command on a command prompt?
Nilesh avatar
nl flag
What if I don't want to quote a variable but want to escape spaces with `\ `? As per case it might be not be suitable for quoting variable in all cases
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.