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 ...

325
votes
3answers
94k views

Difference between single and double quotes in Bash

In Bash, what are the differences between single quotes ('') and double quotes ("")?
80
votes
4answers
21k 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" ] ...
1049
votes
13answers
1.2m 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 ...
3875
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 ...
158
votes
7answers
327k 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 ...
38
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 ];...
387
votes
4answers
274k 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 ...
770
votes
29answers
645k views

Setting environment variables in OS X?

What is the proper way to modify environment variables like PATH in OSX? I've looked on Google a little bit and found 3 different files to edit: /etc/paths~/.profile~/.tcshrcI don't even have ...
505
votes
33answers
578k 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; ...
46
votes
4answers
52k 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 ...
1485
votes
32answers
1.6m 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 ...
1289
votes
28answers
1.0m 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 ...
1587
votes
35answers
1.1m 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 ...
872
votes
8answers
336k 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 ...
118
votes
6answers
110k 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 ...
1023
votes
17answers
725k 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; ...
1712
votes
15answers
841k 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 ...
6
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:...
1544
votes
30answers
451k 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.
130
votes
7answers
395k 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 ...
706
votes
19answers
303k 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\'...
843
votes
12answers
323k 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 ...
556
votes
6answers
286k 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?
913
votes
27answers
895k views

How to output MySQL query results in CSV format?

Is there an easy way to run a MySQL query from the Linux command line and output the results in CSV format?Here's what I'm doing now:mysql -u uid -ppwd -D dbname << EOQ | sed -e 's/ /...
82
votes
9answers
101k 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 ...
499
votes
9answers
1.2m 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:...
178
votes
6answers
178k 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 ...
908
votes
11answers
1.0m 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: ...
397
votes
12answers
743k 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"==...
241
votes
14answers
212k views

Escape a string for a sed replace pattern

In my bash script I have an external (received from user) string, which I should use in sed pattern.REPLACE="<funny characters here>"sed "s/KEYWORD/$REPLACE/g"How can I escape the $REPLACE ...

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