Questions tagged [bash]

Questions specific to GNU’s Bourne Again SHell, as opposed to other Bourne/POSIX shells. For questions about Unix shells in general, use the /shell tag instead. For shell scripts with errors/syntax errors, please check them with the shellcheck program (or in the web shellcheck server at before posting here.

Filter by
Sorted by
Tagged with
374 votes
6 answers

Why does my shell script choke on whitespace or other special characters?

… or an introductory guide to robust filename handling and other string passing in shell scripts. I wrote a shell script which works well most of the time. But it chokes on some inputs (e.g. on some ...
Gilles 'SO- stop being evil''s user avatar
179 votes
1 answer

When is double-quoting necessary?

The old advice used to be to double-quote any expression involving a $VARIABLE, at least if one wanted it to be interpreted by the shell as one single item, otherwise, any spaces in the content of $...
kjo's user avatar
  • 15.3k
272 votes
4 answers

Security implications of forgetting to quote a variable in bash/POSIX shells

If you've been following for a while, you should hopefully know by now that leaving a variable unquoted in list context (as in echo $var) in Bourne/POSIX shells (zsh being the ...
Stéphane Chazelas's user avatar
184 votes
6 answers

How can we run a command stored in a variable?

$ ls -l /tmp/test/my\ dir/ total 0 I was wondering why the following ways to run the above command fail or succeed? $ abc='ls -l "/tmp/test/my dir"' $ $abc ls: cannot access '"/tmp/test/my': No ...
Tim's user avatar
  • 101k
109 votes
4 answers

Understanding "IFS= read -r line"

I obviously understand that one can add value to internal field separator variable. For example: $ IFS=blah $ echo "$IFS" blah $ I also understand that read -r line will save data from stdin to ...
Martin's user avatar
  • 7,496
142 votes
4 answers

Is there a ".bashrc" equivalent file read by all shells?

Is ~/.bashrc the only place to specify user specific environment variables, aliases, modifications to PATH variable, etc? I ask because it seems that ~/.bashrc seems to be bash-only, but other ...
Stefan's user avatar
  • 25.2k
20 votes
2 answers

Brackets in if condition: why am I getting syntax errors without whitespace?

I am using the below script to move two days back when script runs at starting two days of the year and also check first and second days of every month and move two days back. if [$month="01"...
Kumar1's user avatar
  • 341
126 votes
3 answers

How can I assign the output of a command to a shell variable?

I want to assign the result of an expression (i.e., the output from a command) to a variable and then manipulate it – for example, concatenate it with a string, then echo it.  Here's what I've got: #!/...
Nathan G.'s user avatar
  • 1,408
430 votes
16 answers

In Bash, when to alias, when to script and when to write a function?

Noone should need 10 years for asking this question, like I did. If I were just starting out with Linux, I'd want to know: When to alias, when to script and when to write a function? Where aliases are ...
ixtmixilix's user avatar
  • 13.2k
1359 votes
12 answers

How to correctly add a path to PATH?

I'm wondering where a new path has to be added to the PATH environment variable. I know this can be accomplished by editing .bashrc (for example), but it's not clear how to do this. This way: export ...
Paolo's user avatar
  • 17.3k
72 votes
1 answer

What is the difference between the "...", '...', $'...', and $"..." quotes in the shell?

Sometimes I see shell scripts use all of these different ways of quoting some text: "...", '...', $'...', and $"...". Why are there so many different kinds of quote being used? Do ...
Michael Homer's user avatar
151 votes
3 answers

Have backticks (i.e. `cmd`) in *sh shells been deprecated?

I've seen this comment many times on Unix & Linux as well as on other sites that use the phrasing "backticks have been deprecated", with respect to shells such as Bash & Zsh. Is this ...
slm's user avatar
  • 369k
128 votes
4 answers

Why does parameter expansion with spaces without quotes work inside double brackets "[[" but not inside single brackets "["?

I'm confused with using single or double brackets. Look at this code: dir="/home/mazimi/VirtualBox VMs" if [[ -d ${dir} ]]; then echo "yep" fi It works perfectly although the string contains a ...
Majid Azimi's user avatar
  • 3,088
111 votes
4 answers

What is the difference between sourcing ('.' or 'source') and executing a file in bash?

What's the difference between executing a script like this: ./ and executing a script like this: . I tried a simple, two-line script to see if I could find if there was a ...
Nathan's user avatar
  • 1,243
200 votes
14 answers

Batch renaming files

I have a directory full of images: image0001.png image0002.png image0003.png ... And I would like a one-liner to rename them to (say). 0001.png 0002.png 0003.png ... How do I do this?
Internet man's user avatar
  • 3,611
52 votes
5 answers

How can I use $variable in a shell brace expansion of a sequence?

I want to use $var in a shell brace expansion with a range, in bash. Simply putting {$var1..$var2} doesn't work, so I went "lateral"... The following works, but it's a bit kludgey. # remove the ...
Peter.O's user avatar
  • 32.9k
93 votes
4 answers

How can I make environment variables "exported" in a shell script stick around?

I have multiple Amazon EC2 accounts and want to quickly be able to switch variables, such as $EC2_HOME, using a script. I have have a shell script set up like this: #!/bin/sh export EC2_HOME=/home/...
cwd's user avatar
  • 45.2k
30 votes
3 answers

Which shell interpreter runs a script with no shebang?

Suppose the default shell for my account is zsh but I opened the terminal and fired up bash and executed a script named, which shell interpreter would be used to execute the script, zsh or ...
7_R3X's user avatar
  • 1,174
555 votes
7 answers

What is the difference between the Bash operators [[ vs [ vs ( vs ((?

I am a little bit confused on what do these operators do differently when used in bash (brackets, double brackets, parenthesis and double parenthesis). [[ , [ , ( , (( I have seen people use them on ...
RetroCode's user avatar
  • 5,699
710 votes
24 answers

Preserve bash history in multiple terminal windows

I consistently have more than one terminal open. Anywhere from two to ten, doing various bits and bobs. Now let's say I restart and open up another set of terminals. Some remember certain things, some ...
Oli's user avatar
  • 16k
249 votes
11 answers

Terminal prompt not wrapping correctly

I have an issue where if I type in very long commands in bash the terminal will not render what I'm typing correctly. I'd expect that if I had a command like the following: username@someserver ~/...
Muricula's user avatar
  • 2,600
148 votes
5 answers

How to escape quotes in the bash shell?

I'm having trouble with escaping characters in bash. I'd like to escape single and double quotes while running a command under a different user. For the purposes of this question let's say I want to ...
m33lky's user avatar
  • 2,565
147 votes
11 answers

Script to change current directory (cd, pwd)

I want to run a script to simply change the current working directory: #!/bin/bash cd web/www/project But, after I run it, the current pwd remains unchanged! How can I do that?
Sony Santos's user avatar
  • 1,573
72 votes
4 answers

Order of redirections

I don't quite understand how the computer reads this command. cat file1 file2 1> file.txt 2>&1 If I understand, 2>&1 simply redirect Standard Error to Standard Output. By that ...
iDontKnowBetter's user avatar
47 votes
1 answer

How do ${0##*/} and ${0%/*} work?

I'm quite confused about the following regular expressions I found in a shell script: ${0##*/} ${0%/*} How do they work?
user785099's user avatar
14 votes
2 answers

Use a shell variable in awk

Here is my script (to find the files that contain a specified pattern): find . -type f \ -exec awk -v vawk="$1" '/'"$vawk"'/ {c++} c>0 { print ARGV[1]; exit 0 } END { if (! c) {exit 1}}' \{\} \...
Nicolas's user avatar
  • 419
233 votes
9 answers

Looping through files with spaces in the names? [duplicate]

I wrote the following script to diff the outputs of two directores with all the same files in them as such: #!/bin/bash for file in `find . -name "*.csv"` do echo "file = $file"; diff $...
Amir Afghani's user avatar
  • 7,193
33 votes
2 answers

In bash, read after a pipe is not setting values

With ksh I'm using read as a convenient way to separate values: $ echo 1 2 3 4 5 | read a b dump $ echo $b $a 2 1 $ But it fails in Bash: $ echo 1 2 3 4 5 | read a b dump $ echo $b $a $ I didn'...
Emmanuel's user avatar
  • 4,177
133 votes
36 answers

Quick directory navigation in the bash shell

I would like to frequently switch between directories that are in totally unrelated paths, for example /Project/Warnest/docs/ and ~/Dropbox/Projects/ds/test/. But I don't want to type cd /[full-path]/...
saiy2k's user avatar
  • 1,593
367 votes
4 answers

How do I clear Bash's cache of paths to executables?

When I execute a program without specifying the full path to the executable, and Bash must search the directories in $PATH to find the binary, it seems that Bash remembers the path in some sort of ...
Daniel Trebbien's user avatar
98 votes
2 answers

Why is my bash prompt getting bugged when I browse the history? [duplicate]

Often when I start looking at history of commands some of the characters from a command displayed aren't erased, for example: What's happening: prompt$ some_command prompt$ some_commanother_command ...
korda's user avatar
  • 1,571
229 votes
3 answers

Are there naming conventions for variables in shell scripts?

Most languages have naming conventions for variables, the most common style I see in shell scripts is MY_VARIABLE=foo. Is this the convention or is it only for global variables? What about variables ...
Garrett Hall's user avatar
  • 5,271
440 votes
18 answers

How do I change the extension of multiple files?

I would like to change a file extension from *.txt to *.text. I tried using the basename command, but I'm having trouble changing more than one file. Here's my code: files=`ls -1 *.txt` for x in $...
afbr1201's user avatar
  • 4,729
337 votes
5 answers

Why doesn't my Bash script recognize aliases?

In my ~/.bashrc file reside two definitions: commandA, which is an alias to a longer path commandB, which is an alias to a Bash script I want to process the same file with these two commands, so I ...
Zaid's user avatar
  • 10.6k
202 votes
3 answers

Quoting within $(command substitution) in Bash

In my Bash environment I use variables containing spaces, and I use these variables within command substitution. What is the correct way to quote my variables? And how should I do it if these are ...
CousinCocaine's user avatar
288 votes
8 answers

How to determine where an environment variable came from?

I have a Linux instance that I set up some time ago. When I fire it up and log in as root there are some environment variables that I set up but I can't remember or find where they came from. I've ...
Joel's user avatar
  • 2,983
653 votes
4 answers

Using "${a:-b}" for variable assignment in scripts

I have been looking at a few scripts other people wrote (specifically Red Hat), and a lot of their variables are assigned using the following notation VARIABLE1="${VARIABLE1:-some_val}" or some expand ...
Justin Garrison's user avatar
105 votes
9 answers

When would you use an additional file descriptor?

I know you can create a file descriptor and redirect output to it. e.g. exec 3<> /tmp/foo # open fd 3. echo a >&3 # write to it exec 3>&- # close fd 3. But you can do the same ...
dogbane's user avatar
  • 29.6k
89 votes
4 answers

What is the difference between a builtin command and one that is not?

Is there any intrinsic difference between a builtin command and another command which can nominally do the same thing? eg. Do builtins get "special" treatement? ... is there less overhead running ...
Peter.O's user avatar
  • 32.9k
51 votes
4 answers

Execute bash scripts on entering a directory

What is the best way to execute a script when entering into a directory? When I move into a new directory I would like bash to execute the projectSettings.bash script much like RVM does.
Tegra Detra's user avatar
  • 5,006
62 votes
7 answers

Escape a variable for use as content of another script

This question is not about how to write a properly escaped string literal. I couldn't find any related question that isn't about how to escape variables for direct consumption within a script or by ...
Walf's user avatar
  • 1,311
33 votes
4 answers

Quoting in ssh $host $FOO and ssh $host "sudo su user -c $FOO" type constructs

I often end up issuing complex commands over ssh; these commands involve piping to awk or perl one-lines, and as a result contain single quotes and $'s. I have neither been able to figure out a hard ...
Leo Alekseyev's user avatar
69 votes
6 answers

How to echo a bang!

I tried to create a script by echo'ing the contents into a file, instead of opening it with a editor echo -e "#!/bin/bash \n /usr/bin/command args" > .scripts/command The output: bash: !/bin/...
Stefan's user avatar
  • 25.2k
14 votes
2 answers

Why are bash tests so picky about whitespace?

As a primarily Java programmer, I find the bash if-then construct quite confusing, especially regarding whitespace. Can anyone explain why the first one works, but not the second or third? #works if [...
Konrad Höffner's user avatar
9 votes
2 answers

Can I read and write to the same file in Linux without overwriting it? [duplicate]

Possible Duplicate: How can I make iconv replace the input file with the converted output? I frequently connect to amazon ec2 using their public DNS names (
cwd's user avatar
  • 45.2k
8 votes
2 answers

Terminal prompt overwrites current line

I have an issue with my terminal prompt line. When the line is too long it wraps on the same line and then Up arrow makes it look even worse. I have already checked Terminal prompt not wrapping ...
melisc's user avatar
  • 83
324 votes
11 answers

What is the "eval" command in bash?

What can you do with the eval command? Why is it useful? Is it some kind of a built-in function in bash? There is no man page for it..
LanceBaynes's user avatar
  • 40.1k
275 votes
13 answers

Is there a one-liner that allows me to create a directory and move into it at the same time?

I find myself repeating a lot of: mkdir longtitleproject cd longtitleproject Is there a way of doing it in one line without repeating the directory name? I'm on bash here.
methodofaction's user avatar
96 votes
19 answers

How to check if a pipe is empty and run a command on the data if it isn't?

I have piped a line in bash script and want to check if the pipe has data, before feeding it to a program. Searching I found about test -t 0 but it doesn't work here. Always returns false. So how to ...
zetah's user avatar
  • 2,037
80 votes
2 answers

Output from ls has newlines but displays on a single line. Why?

I think I may be overlooking a relatively fundamental point regarding shell. Output from the ls command by default separates output with newlines, but the shell displays the output on a single line. ...
theconnorpower's user avatar

2 3 4 5