Questions tagged [bash]

For questions about scripts written for the Bash command shell. For shell scripts with errors, please check them with the shellcheck program (or in the web shellcheck server at https://shellcheck.net) before posting here. Questions about interactive use of Bash are more likely to be on-topic on ...

360
votes
5answers
110k views

Difference between single and double quotes in Bash

In Bash, what are the differences between single quotes ('') and double quotes ("")?
93
votes
5answers
26k views

When to wrap quotes around a shell variable?

Could someone tell me whether or not I should wrap quotes around variables in a shell script?For example, is the following correct:xdg-open $URL [ $? -eq 2 ]orxdg-open "$URL"[ "$?" -eq "2" ] ...
1158
votes
13answers
1.3m views

How to set a variable to the output of a command in Bash?

I have a pretty simple script that is something like the following:#!/bin/bashVAR1="$1" MOREF='sudo run command against $VAR1 | grep name | cut -c7-'echo $MOREFWhen I run this script from ...
4072
votes
51answers
1.3m views

Getting the source directory of a Bash script from within

How do I get the path of the directory in which a Bash script is located, inside that script?For instance, let's say I want to use a Bash script as a launcher for another application. I want to ...
184
votes
8answers
358k views

How do I use shell variables in an awk script?

I found some ways to pass external shell variables to an awk script, but I'm confused about ' and ".First, I tried with a shell script:$ v=123test$ echo $v123test$ echo "$v"123testThen tried ...
43
votes
4answers
4k views

Why should there be a space after '[' and before ']' in Bash?

I was trying to write a Bash script that uses an if statement.if [$CHOICE -eq 1];The script was giving me errors until I gave a space after [ and before ] as shown below:if [ $CHOICE -eq 1 ];...
416
votes
4answers
284k views

Command not found error in Bash variable assignment

I have this script called test.sh:#!/bin/bashSTR="Hello World"echo $STRwhen I run sh test.sh I get this:test.sh: line 2: STR: command not foundWhat am I doing wrong? I look at extremely ...
59
votes
6answers
75k views

I just assigned a variable, but echo $variable shows something else

Here are a series of cases where echo $var can show a different value than what was just assigned. This happens regardless of whether the assigned value was "double quoted", 'single quoted' or ...
787
votes
30answers
656k views

Setting environment variables on OS X

What is the proper way to modify environment variables like PATH in OS X?I've looked on Google a little bit and found three different files to edit:/etc/paths~/.profile~/.tcshrcI don't ...
583
votes
35answers
641k views

Parsing JSON with Unix tools

I'm trying to parse JSON returned from a curl request, like so:curl 'http://twitter.com/users/username.json' |sed -e 's/[{}]/''/g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; ...
1570
votes
32answers
1.7m views

How do I split a string on a delimiter in Bash?

I have this string stored in a variable:IN="bla@some.com;john@home.com"Now I would like to split the strings by ; delimiter so that I have:ADDR1="bla@some.com"ADDR2="john@home.com"I don't ...
1412
votes
29answers
1.1m views

How do I parse command line arguments in Bash?

Say, I have a script that gets called with this line:http://stackoverflow.com/myscript -vfd http://stackoverflow.com/foo/bar/someFile -o /fizz/someOtherFileor this one:http://stackoverflow.com/myscript -v -f -d -o /fizz/someOtherFile http://stackoverflow.com/foo/bar/someFile What's ...
949
votes
10answers
363k views

Difference between sh and bash

When writing shell programs, we often use /bin/sh and /bin/bash. I usually use bash, but I don't know what's the difference between them. What's main difference between bash and sh?What do we need ...
129
votes
6answers
122k views

A variable modified inside a while loop is not remembered

In the following program, if I set the variable $foo to the value 1 inside the first if statement, it works in the sense that its value is remembered after the if statement. However, when I set the ...
1105
votes
17answers
788k views

How do I iterate over a range of numbers defined by variables in Bash?

How do I iterate over a range of numbers in Bash when the range is given by a variable?I know I can do this (called "sequence expression" in the Bash documentation):for i in {1..5}; do echo $i; ...
1681
votes
35answers
1.2m views

Extract filename and extension in Bash

I want to get the filename (without extension) and the extension separately.The best solution I found so far is:NAME=`echo "$FILE" | cut -d'.' -f1`EXTENSION=`echo "$FILE" | cut -d'.' -f2`This ...
7
votes
1answer
1k views

Why would a correct shell script give a wrapped/truncated/corrupted error message? [duplicate]

I have a shell script with a command that seems like it should work, but instead it fails with an odd wrapped/truncated/corrupted error message. Example:$ ls -l myfile-rw-r----- 1 me me 0 Aug 7 12:...
145
votes
13answers
149k views

How to debug a bash script? [closed]

Is there any way to debug a bash script? E.g something that prints a sort of execution log like "calling line 1", "calling line 2" etc.
1800
votes
15answers
882k views

In the shell, what does “ 2>&1 ” mean?

In a Unix shell, if I want to combine stderr and stdout into the stdout stream for further manipulation, I can append the following on the end of my command:2>&1So, if I want to use head on ...
1654
votes
34answers
488k views

How to check if a program exists from a Bash script?

How would I validate that a program exists, in a way that will either return an error and exit, or continue with the script?It seems like it should be easy, but it's been stumping me.
762
votes
19answers
326k views

How to escape single quotes within single quoted strings?

Let's say, you have a bash alias like:alias rxvt='urxvt'which works fine.However:alias rxvt='urxvt -fg '#111111' -bg '#111111''won't work, and neither will:alias rxvt='urxvt -fg \'#111111\'...
915
votes
11answers
357k views

Make a Bash alias that takes a parameter?

I used to use CShell (csh), which lets you make an alias that takes a parameter. The notation was something likealias junk="mv \\!* ~/.Trash"In Bash, this does not seem to work. Given that Bash ...
130
votes
7answers
411k views

What's the difference between .bashrc, .bash_profile, and .environment?

I've used a number of different *nix-based systems of the years, and it seems like every flavor of Bash I use has a different algorithm for deciding which startup scripts to run. For the purposes of ...
95
votes
10answers
114k views

Dynamic variable names in Bash

I am confused about a bash script.I have the following code:function grep_search() {magic_way_to_define_magic_variable_$1=`ls | tail -1`echo $magic_variable_$1}I want to be able to ...
576
votes
6answers
297k views

How to use double or single brackets, parentheses, curly braces

I am confused by the usage of brackets, parentheses, curly braces in Bash, as well as the difference between their double or single forms. Is there a clear explanation?
561
votes
9answers
1.3m views

Read a file line by line assigning the value to a variable

I have the following .txt file:MarcoPaoloAntonioI want to read it line-by-line, and for each line I want to assign a .txt line value to a variable. Supposing my variable is $name, the flow is:...
991
votes
11answers
1.1m views

Looping through the content of a file in Bash

How do I iterate through each line of a text file with Bash?With this script:echo "Start!"for p in (peptides.txt)doecho "${p}"doneI get this output on the screen:Start!http://stackoverflow.com/runPep.sh: ...
20
votes
2answers
6k views

Are shell scripts sensitive to encoding and line endings?

I am making a NW.js app on Mac, and want to run the app in dev mode by double-clicking on an icon. First step, I'm trying to make my shell script work.Using VSCode on Windows (I wanted to gain time),...
39
votes
4answers
45k views

sed substitution with bash variables

Trying to change values in a text file using sed in a bash script with the line,sed 's/draw($prev_number;n_)/draw($number;n_)/g' file.txt > tmpThis will be in a for loop. Not sure why it's not ...
454
votes
12answers
812k views

How do I compare two string variables in an 'if' statement in Bash?

I'm trying to get an if statement to work in Bash (using Ubuntu):#!/bin/bashs1="hi"s2="hi"if ["$s1"=="$s2"]thenecho matchfiI've tried various forms of the if statement, using [["$s1"==...
24
votes
4answers
4k views

Why does shell ignore quotes in arguments passed to it through variables?

This works as advertised:# example 1#!/bin/bashgrep -ir 'hello world' .This doesn't:# example 2#!/bin/bashargumentString="-ir 'hello world'"grep $argumentString .Despite 'hello world' ...
264
votes
22answers
359k views

How to convert DOS/Windows newline (CRLF) to Unix newline (LF) in a Bash script?

How can I programmatically (i.e., not using vi) convert DOS/Windows newlines to Unix?The dos2unix and unix2dos commands are not available on certain systems. How can I emulate these with commands ...
649
votes
20answers
1.1m views

How to permanently set $PATH on Linux/Unix?

I'm trying to add a directory to my path so it will always be in my Linux path. I've tried:export PATH=$PATH:/path/to/dirThis works, however each time I exit the terminal and start a new terminal ...
190
votes
6answers
187k views

How do I write a bash script to restart a process if it dies?

I have a python script that'll be checking a queue and performing an action on each item:# checkqueue.pywhile True:check_queue()do_something()How do I write a bash script that will check if ...
69
votes
3answers
51k views

Why should eval be avoided in Bash, and what should I use instead?

Time and time again, I see Bash answers on Stack Overflow using eval and the answers get bashed, pun intended, for the use of such an "evil" construct. Why is eval so evil?If eval can't be used ...
700
votes
6answers
492k views

How to iterate over arguments in a Bash script

I have a complex command that I'd like to make a shell/bash script of. I can write it in terms of $1 easily:foo $1 args -o $1.extI want to be able to pass multiple input names to the script. What'...
205
votes
6answers
35k views

What is the difference between $(command) and `command` in shell programming?

To store the output of a command as a variable in sh/ksh/bash, you can do eithervar=$(command)orvar=`command`What's the difference if any between the two methods?
1127
votes
31answers
951k views

How to check if a variable is set in Bash?

How do I know if a variable is set in Bash?For example, how do I check if the user gave the first parameter to a function?function a {# if $1 is set ?}
1228
votes
6answers
604k views

How can I redirect and append both stdout and stderr to a file with Bash?

To redirect stdout to a truncated file in Bash, I know to use:cmd > file.txtTo redirect stdout in Bash, appending to a file, I know to use:cmd >> file.txtTo redirect both stdout and ...
250
votes
7answers
227k views

Expansion of variable inside single quotes in a command in Bash

I want to run a command from a bash shell script which has single quotes and some other commands inside the single quotes and a variable.e.g. repo forall -c '....$variable'In this format, $ is ...
1855
votes
21answers
1.5m views

How to check if a string contains a substring in Bash

I have a string in Bash:string="My string"How can I test if it contains another string?if [ $string ?? 'foo' ]; thenecho "It's there!"fiWhere ?? is my unknown operator. Do I use echo and ...
110
votes
15answers
211k views

How to parse XML in Bash?

Ideally, what I would like to be able to do is:cat xhtmlfile.xhtml |getElementViaXPath --path='/html/head/title' |sed -e 's%(^<title>|</title>$)%%g' > titleOfXHTMLPage.txt
649
votes
4answers
251k views

What are the special dollar sign shell variables?

In Bash, there appear to be several variables which hold special, consistently-meaning values. For instance,http://stackoverflow.com/myprogram &; echo $!will return the PID of the process which backgrounded ...
170
votes
8answers
81k views

Iterate over a list of files with spaces

I want to iterate over a list of files. This list is the result of a find command, so I came up with:getlist() {for f in $(find . -iname "foo*")doecho "File found: $f"# do something ...
19
votes
3answers
3k views

Pass commands as input to another command (su, ssh, sh, etc)

I have a script where I need to start a command, then pass some additional commands as commands to that command. I triedsuecho I should be root now:who am Iexitecho done.... but it doesn't ...
2201
votes
27answers
2.8m views

How to concatenate string variables in Bash

In PHP, strings are concatenated together as follows:$foo="Hello";$foo .=" World";Here, $foo becomes "Hello World".How is this accomplished in Bash?
95
votes
25answers
89k views

An efficient way to transpose a file in Bash

I have a huge tab-separated file formatted like thisX column1 column2 column3row1 0 1 2row2 3 4 5row3 6 7 8row4 9 10 11I would like to transpose it in an efficient way using only bash commands ...
169
votes
8answers
323k views

Running Bash commands in Python

On my local machine I run a python script which contains this linebashCommand="cwm --rdf test.rdf --ntriples > test.nt"os.system(bashCommand)This works fine.Then I run the same code on a ...
478
votes
6answers
169k views

When do we need curly braces around shell variables?

In shell scripts, when do we use {} when expanding variables?For example, I have seen the following:var=10 # Declare variableecho "${var}" # One use of the variableecho "$var" # ...
457
votes
6answers
214k views

Capturing multiple line output into a Bash variable

I've got a script 'myscript' that outputs the following:abcdefghiin another script, I call:declare RESULT=$(http://stackoverflow.com/myscript)and $RESULT gets the valueabc def ghiIs there a way to store the ...

153050per page
angop.ao, elkhabar.com, noa.al, afghanpaper.com, bbc.com, time.com, cdc.gov, nih.gov, xnxx.com, github.com,