Linked Questions
805 questions linked to/from Why does my shell script choke on whitespace or other special characters?
86
votes
3
answers
283k
views
Why I can't escape spaces on a bash script? [duplicate]
I'm trying to escape the space char for a path in Bash, but neither using a backslash or quotes works.
.sh script:
ROOT="/home/hogar/Documents/files/"
FILE=${ROOT}"bdd.encrypted"
DESTINATION="/home/...
40
votes
4
answers
230k
views
How do I echo a string with multiple spaces in bash "untouched"? [duplicate]
I assigned a var like this:
MYCUSTOMTAB=' '
But using it in echo both:
echo $MYCUSTOMTAB"blah blah"
or
echo -e $MYCUSTOMTAB"blah blah"
just returns a single space and the rest of the string:
...
53
votes
2
answers
206k
views
How to pass argument with spaces to a shell script function? [duplicate]
I'm trying to pass multiple argument to a function, but one of them is consist of two words and I want shell function to deal with it as one arg:
args=("$@")
function(){
echo ${args[0]}
echo ${args[...
21
votes
1
answer
39k
views
Preserve formatting when command output is sent to a variable? [duplicate]
I have a bash script. If I run this command to:
grep for certain patterns,
transform the output,
sort the output
dedupe the output
then I get one grep find per line in terminal
LC_ALL=C grep -o --...
21
votes
1
answer
40k
views
Export path with space in directory name [duplicate]
I have a path "/third/party/city of las vegas"
when cd the path I use "/third/party/city of las vegas".
In .profile file I have exported the path to a variable as follows
export clv="/third/party/...
9
votes
2
answers
21k
views
How to treat strings with spaces as a whole in bash? [duplicate]
I'd like to build an error handler which will make an empty file marked with the error_occur time.
The core idea is to use the result of date command as a parameter.
I did:
time_stamp=$(date)
touch ...
3
votes
3
answers
6k
views
how to get the basename of complicated files [duplicate]
Here is an example of file path:
/isf/GCM/VPfig/Aas/AR/ClCo el Doma Republic/VMN CRTro.txt
What I want to get is the file basename:
VMN CRTro.txt
So I try the following:
echo /isf/GCM/VPfig/Aas/AR/...
10
votes
2
answers
2k
views
Echoing a string in bash with brackets surrounding it returns 'b' [duplicate]
$ a='"apple","ball","cat"'
$ a='['$a
$ echo $a
["apple","ball","cat"
$ a=$a']'
$ echo $a
b
I'm stumped hard by the result b while I expect to see ["apple,"ball","cat"]. What am I missing here?
This ...
9
votes
3
answers
4k
views
File name beginning with - (dash) [duplicate]
I have file called -l in my directory
now I tried to do
for i in *; do stat -c "%s %n" "$i"; done
it lists all files with sizes but in the middle of the output there is something like
395 koko.pub
...
7
votes
2
answers
113k
views
Creating directory with space in name [duplicate]
I'm trying to create a directory which has space in name e.g. "user test"
when I fire mkdir -p "user test" it works for me.
When I put "user test" in variable mkdir fails, it creates "user and test" ...
1
vote
2
answers
55k
views
How to use cp command in bash script to copy files with spaces ans $ characters in it? [duplicate]
I am trying to copy some files with spaces and $, @ symbols in their file names in a bash script but the script fails to copy the files stating it cannot find the file. I can see that it is treating ...
9
votes
1
answer
6k
views
Prevent glob expansion in foo="*"; echo $foo [duplicate]
In Bash, when I do:
foo="*"
echo $foo
It expands * to the contents of the current folder. How do I make sure it just prints a literal *?
The same, by the way, happens with a regular echo "$foo", it ...
4
votes
2
answers
24k
views
Why does a sed command work interactively but not in my script? [duplicate]
SED command not replacing in bash under Debian, but works in command line.
Works in command line:
sed -i 's|/dev/disk/by-label/SR6D4|/dev/disk/by-label/SR4D4|g' /etc/my/config.xml
Not working in ...
6
votes
1
answer
10k
views
Read space as input in shell script [duplicate]
How can I give space " " as input in shell script ?
Ex :
echo " Enter date for grep... Ex: Oct 6 [***Double space is important for single date***] (or) Oct 12 "
read mdate
echo $mdate
I get the ...
3
votes
1
answer
20k
views
basename extra operand error [duplicate]
I have a script that will try to extract the file's base name and then I do additional work with it. Only when using this script with a file with this naming convention (including spaces and ...