This question already has an answer here:
In bash, how do I expand a wildcard while it's inside double quotes?
(1 answer)
Closed 2 years ago.
I’m trying to execute a shell command that deletes the content of the folder. This was executed on macOS Catalina.
This command works:
rm -r /path/folder/*
But this won’t work:
rm -r “/path/folder/*”
I wonder what is the effect of those quotes. The path changes because it is executed from my program. I added quotes in the path to make up for spaces in the path if it exists. Then I tested it in actual command line to check and it behaves like that.
The quotes are to block-escape paths containing space characters.
In case of an asterisk as a wildcard you have to keep the asterisk out of the block
rm -r "/path/my great folder"/*
An alternative is to escape each space character with a backslash.
rm -r /path/my\ great\ folder/*
Related
This question already has answers here:
How do I deal with a filename that starts with the hyphen (-) character?
(5 answers)
Closed 12 months ago.
I was attempting to use the xxd command in bash, and used the -r argument, but forgot to add the in-file and out-file. I now have a useless file named -r and I cant remove it due to it being an argument for almost any command that I can think of that would be able to delete files.
Is there any other way for me to remove this file?
To remove a file whose name starts with a -, use:
rm -- -r
or
rm ./-r
-- is used to signify the end of command options.
This question already has answers here:
How to cd into a directory with space in the name?
(18 answers)
Closed 2 years ago.
I could not cd to a directory, whose path name contains escaped spaces, that is represented by a variable.
The following command does not work.
~$ VAR="/mnt/e/documents/my\ files"
~$ cd $VAR
-bash: cd: too many arguments
However, when I use the path name as it is instead of assigning it to a variable, it works.
~$ cd /mnt/e/documents/my\ files
cd /mnt/e/documents/my files$
How to use cd with a variable as its argument, where the variable is a string with spaces that are escaped.
You have two problems:
Remove the backslash in the middle of my\ files. You only need to escape that space when you're not quoting it.
Add quotes around $VAR: cd "$VAR", otherwise it looks as though you are providing two parameters: "/mnt/e/documents/my" and "files"
This question already has answers here:
How to escape single quotes within single quoted strings
(25 answers)
Closed 2 years ago.
i have a file test.txt with data init=6'b000000; and i want to replace it with init=6'b110111; by using vim script in a bash file. I'm getting error
I'm using following command:
vim -c '%s/init=6'b000000;/init=6'b110111;/g | write | quit' test.txt
this works perfectly in vim, not in bash .
This has nothing to do with Vim, you cannot embed a single quote inside a pair of other single quotes. The shell parses the command line arguments before passing them to the invoked command and it just cannot deal with the inner single quote in the way you have defined.
The literal single quote inside need to be preserved before passing to the command. So use double quote and escape the inner quote
vim -c "%s/init=6\'b000000;/init=6\'b110111;/g | write | quit" file
or use the single quote, but include a multi-level double quote inside
vim -c '%s/init=6'"\'"'b000000;/init=6'"\'"'b110111;/g | write | quit' file
This question already has answers here:
How can I preserve quotes in printing a bash script's arguments
(7 answers)
Closed 4 years ago.
I have a script that logs the user argument list. This list is later processed by getopt.
If the script is started like:
./script.sh -a 'this is a sentence' -b 1
... and then I save "$#", I get:
-a this is a sentence -b 1
... without the single quotes. I think (because of the way Bash treats quotes) these are removed and are not available to the script.
For logging accuracy, I'd like to include the quotes too.
Can the original argument list be obtained without needing to quote-the-quotes?
No, there is no way to obtain the command line from before the shell performed whitespace tokenization, wildcard expansion, and quote removal on it.
If you want to pass in literal quotes, try
./script.sh '"-a"' '"this is a sentence"' '"-b"' '"1"'
Notice also how your original command line could have been written
'./script.sh' '-a' 'this is a sentence' '-b' '1'
This question already has answers here:
Bash: Expand braces and globs with spaces in filenames?
(3 answers)
Closed 6 years ago.
Why this command works:
sudo rm -rf ${server_tomcatHomeDir}temp/*
While the same, but with quoted arg, doesn't work:
sudo rm -rf "${server_tomcatHomeDir}temp/*"
It might be related only to quotes, but why this command works even with quoted args:
sudo cp "$HOME/${artifact}" "${server_tomcatHomeDir}/webapps/${webapp}"
I can't understand. Can anyone clarify?
I just want to use quotes in the first command to prevent globbing and word splitting.
The * here is a pathname expansion (glob) token, that will match any number of characters (including zero) in filename, and it is not expanded when put inside quotes, it is treated literally then.
Do:
sudo rm -rf "${server_tomcatHomeDir}"temp/*
Note that, it is almost always a good idea to quote your variables, especially when dealing with file names to avoid word splitting, and pathname expansion.
You have to put the * outside of the quotes, so
sudo rm -rf "${server_tomcatHomeDir}temp/"*