Skip to main content
6403 votes
82 answers
2.8m views

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 ...
4550 votes
35 answers
3.5m views

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

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
3665 votes
22 answers
3.7m views

How do I print a newline? This merely prints \n: echo -e "Hello,\nWorld!" Output: Hello,\nWorld!
Sergey's user avatar
  • 50.2k
3657 votes
30 answers
3.4m views

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
  • 40.4k
3611 votes
31 answers
5.3m views

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
  • 68.3k
3253 votes
19 answers
2.0m views

To combine stderr and stdout into the stdout stream, we append this to a command: 2>&1 For example, the following command shows the first few errors from compiling main.cpp: g++ main.cpp 2>&...
Tristan Havelick's user avatar
3214 votes
38 answers
1.3m views

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
  • 37.7k
3032 votes
40 answers
3.9m views

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
  • 80.5k
2918 votes
40 answers
2.4m views

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 -...
ibz's user avatar
  • 47.3k
2851 votes
34 answers
2.2m views

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

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
  • 69.9k
2517 votes
38 answers
2.5m views

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
  • 193k
2507 votes
16 answers
3.1m views

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
  • 25.5k
2418 votes
22 answers
2.3m views

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

15 30 50 per page
1
2 3 4 5
10457