Tagged Questions
The Bourne-Again SHell (Bash) is a successor to the Bourne shell (sh). Bash is the default shell in many Linux distributions.
962
votes
36answers
284k views
Can a Bash script tell what directory it's stored in?
How do I get the path of the directory in which a bash script is located FROM that bash script?
For instance, lets say I want to use a bash script as a launcher for another application. I want to ...
430
votes
11answers
286k views
How do I tell if a file does not exist in bash?
I've used the following script to see if a file exists:
#!/bin/bash
FILE=$1
if [ -f $FILE ];
then
echo "File $FILE exists."
else
echo "File $FILE does not exist."
fi
What's the correct ...
327
votes
18answers
228k 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 ...
320
votes
19answers
339k views
How do I split a string on a delimiter in bash?
How do I split a string based 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
...
315
votes
18answers
227k views
Setting environment variables in 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 3 different files to edit:
/etc/paths
~/.profile
~/.tcshrc
I don't even have ...
274
votes
14answers
246k views
String contains in bash
Using bash, I have a string:
string=`echo 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. Do I ...
261
votes
12answers
54k views
Why are scripting languages (e.g. Perl, Python, Ruby) not suitable as shell languages? [closed]
What are the differences between shell languages like bash, zsh, fish and the scripting languages above that makes them more suitable for the shell?
When using the command line the shell languages ...
250
votes
16answers
72k views
Check if a program exists from a bash script
How would I validate that a program exists? which would then either return an error and exit or continue with the script.
It seems like it should be easy, but it's been stumping me.
237
votes
11answers
107k views
Calling Bash Commands From Ruby
How do I call console/bash commands from inside of a Ruby Program? Also, how do I get output from these commands back into my program?
237
votes
10answers
273k views
How can I concatenate string variables in Bash?
In PHP I would add strings together like this:
$foo = "Hello";
$foo .= " World";
So $foo would be "Hello World"
How would I do that in Bash?
228
votes
23answers
81k views
How to count all the lines of code in a directory recursively?
We've got a PHP application and want to count all the lines of code under a specific directory and its subdirectories. We don't need to ignore comments, as we're just trying to get a rough idea.
wc ...
219
votes
12answers
61k views
How Can I Remove .DS_Store Files From A Git Repository?
How can I remove those annoying Mac OS X .DS_Store files from a Git repository?
200
votes
11answers
117k views
Converting string to lower case in Bash shell scripting
Is there a way in Bash shell scripting so that I can convert a string into lower case string?
For example,
if $a = "Hi all"
I want to convert it to
$a = "hi all"
199
votes
14answers
154k 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/ ...
189
votes
10answers
104k 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; ...