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
366 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 ...
171 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 $...
  • 14k
264 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 ...
155 votes
5 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 ...
  • 94.8k
102 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 ...
  • 6,972
138 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 ...
  • 24.3k
114 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: #!/...
  • 1,288
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"...
  • 341
424 votes
16 answers

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

It's taken me almost 10 years of Linux usage to ask this question. It was all trial and error and random late-night internet surfing. But people shouldn't need 10 years for this. If I were just ...
  • 13.2k
1309 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 ...
  • 16.5k
149 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 ...
  • 356k
68 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 ...
117 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 ...
  • 2,948
194 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?
  • 3,483
104 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 ...
  • 1,173
86 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/...
  • 43.2k
51 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 ...
  • 31.9k
512 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 ...
  • 5,269
692 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 ...
  • 15.5k
26 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 ...
  • 1,094
236 votes
10 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 ~/...
  • 2,470
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?
130 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?
  • 1,403
13 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}}' \{\} \...
  • 399
217 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 $...
  • 6,913
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'...
  • 4,137
64 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 ...
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]/...
  • 1,593
351 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 ...
92 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 ...
  • 1,501
209 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 ...
  • 4,951
388 votes
17 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 on changing more than one file. Here's my code: files=`ls -1 *.txt` for x in ...
  • 4,209
311 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 ...
  • 10.2k
260 votes
9 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 ...
  • 2,703
194 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 ...
98 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 ...
  • 28.1k
637 votes
3 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 ...
86 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 ...
  • 31.9k
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 ...
13 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 [...
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 (
  • 43.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 ...
  • 83
297 votes
10 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..
  • 37.9k
260 votes
12 answers

Execute a command once per line of piped input?

I want to run a java command once for every match of ls | grep pattern -. In this case, I think I could do find pattern -exec java MyProg '{}' \; but I'm curious about the general case - is there an ...
  • 3,513
249 votes
12 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.
66 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/...
  • 24.3k
48 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.
  • 4,838
76 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. ...
59 votes
3 answers

unix, difference between path starting with '/' and '//' [duplicate]

In unix/linux, any number of consecutive forwardslashes in a path is generally equivalent to a single forwardslash. eg. $ cd /home/shum $ pwd /home/shum $ cd /home//shum $ pwd /home/shum $ cd /home///...
  • 1,315
39 votes
3 answers

What is the difference between [[ $a == z* ]] and [ $a == z* ]?

Is there is any difference between these two. [[ $a == z* ]] and [ $a == z* ] Can I have an example where they would have different outputs? Furthermore, how does the working of [[ ]] differs ...
  • 7,483

2 3 4 5