Skip to main content
The 2024 Developer Survey results are live! See the results

Questions tagged [bash]

**** IMPORTANT NOTE: Do not ask a bash question until after you have copy/pasted your script into http://shellcheck.net and fixed all of the issues it tells you about. **** This tag is for questions about scripts written for the Bash command shell. Questions about the interactive use of Bash are more likely to be on-topic on Unix & Linux Stack Exchange or Super User than on Stack Overflow.

6284 votes
77 answers
2.6m views

How do I get the directory where a Bash script is located from within the script itself?

How do I get the path of the directory in which a Bash script is located, inside that script? I want to use a Bash script as a launcher for another application. I want to change the working directory ...
4470 votes
35 answers
3.3m views

How do I check if a directory exists or not in a Bash shell script?

What command checks if a directory exists or not within a Bash shell script?
Grundlefleck's user avatar
4033 votes
21 answers
3.1m views

How do I tell if a file does not exist in Bash?

This checks if a file exists: #!/bin/bash FILE=$1 if [ -f $FILE ]; then echo "File $FILE exists." else echo "File $FILE does not exist." fi How do I only check if the ...
Bill the Lizard's user avatar
3566 votes
30 answers
3.2m 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' ]; then echo "It's there!" fi Where ?? is my unknown operator. ...
davidsheldon's user avatar
  • 39.5k
3554 votes
31 answers
5.1m 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?
Strawberry's user avatar
  • 67.3k
3536 votes
24 answers
3.5m views

Echo newline in Bash prints literal \n

How do I print a newline? This merely prints \n: $ echo -e "Hello,\nWorld!" Hello,\nWorld!
Sergey's user avatar
  • 48.8k
3183 votes
19 answers
1.8m views

What does " 2>&1 " mean?

To combine stderr and stdout into the stdout stream, we append this to a command: 2>&1 e.g. to see the first few errors from compiling g++ main.cpp: g++ main.cpp 2>&1 | head What does ...
Tristan Havelick's user avatar
3148 votes
39 answers
1.2m views

How can I 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.
gregh's user avatar
  • 36.9k
2949 votes
39 answers
3.6m views

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

I have this string stored in a variable: IN="[email protected];[email protected]" Now I would like to split the strings by ; delimiter so that I have: ADDR1="[email protected]" ADDR2="[email protected]" I don't ...
stefanB's user avatar
  • 79k
2851 votes
40 answers
2.3m 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 ...
ibz's user avatar
  • 45.9k
2783 votes
33 answers
2.0m views

How to change the output color of echo in Linux

I am trying to print a text in the terminal using echo command. I want to print the text in a red color. How can I do that?
satheesh.droid's user avatar
2577 votes
44 answers
2.1m views

How do I parse command line arguments in Bash?

Say, I have a script that gets called with this line: ./myscript -vfd ./foo/bar/someFile -o /fizz/someOtherFile or this one: ./myscript -v -f -d -o /fizz/someOtherFile ./foo/bar/someFile What's the ...
Redwood's user avatar
  • 68.5k
2441 votes
38 answers
2.3m 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 ? }
prosseek's user avatar
  • 188k
2436 votes
16 answers
2.9m views

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

I have a pretty simple script that is something like the following: #!/bin/bash VAR1="$1" MOREF='sudo run command against $VAR1 | grep name | cut -c7-' echo $MOREF When I run this script from the ...
John's user avatar
  • 24.8k
2305 votes
21 answers
2.1m views

Loop through an array of strings in Bash?

I want to write a script that loops through 15 strings (array possibly?) Is that possible? Something like: for databaseName in listOfNames then # Do something end
Mo.'s user avatar
  • 41.7k

15 30 50 per page
1
2 3 4 5
10419