bash script

How to stream the output of a command in a bash script?

Adding a command in a script will simply print the command output to the console.
But what if I want to filter the output, add to it, act upon it, etc.?

Right now, I simply assign the output of the command to a variable, print the variable and grep the variable to find the bits that interest me.
But if the command takes a long time to run, the user won't see any output until the command has finished running.
I want to do all of the above, while the user is seeing the output and the process progresses...

bash: switch case flow control

case $var in
    pattern1 )
        statements ;;
    pattern2 )
        statements ;;
        statemest ;;

Note the two consecutive semi-column ";;" which are a necessary part of the syntax!
The *) case is the default choice if none other apply.

format() {
        case "$1" in
        'banana' | 'apple' )
                echo 'It is a fruit.';;
        'cat' )
                echo 'It is an animal.';;

print_category "banana"
print_category "cat"
print_category "apple"

bash: working with strings

string length:




# E.g. PWD = "/home/user/scripts/myproject"
#$project = "myproject"
echo "$project"


Bash script: how does the $ wildcard work in git script?

I checked that the following is true, but I still don't understand why or how it works:


git checkout production.release.4.2
production_branch=`git branch | grep "production"`

# May not give the expected result:
# the character $ is being treated as a wildcard and will be expanded, so that the output may include all files in the directory:
echo $production_branch
# Using double quotes solve the problem:
echo "$production_branch"

git: git branch name programmatically

In a bash script, you can get the current branch's name programmatically thus:


branch_name=$(git symbolic-ref -q HEAD)
echo $branch_name
echo $branch_name
echo $branch_name

Beware, to find a branch name (whether checked out or not) according to a pattern:


git checkout production.release.4.2
production_branch=`git branch | grep "production"`

# May not give the expected result:

bash: validating a file (existence, type...)

How to validate a file?


if [ -f $FILE ];
   echo "The file $FILE exists."

# For a directory, use -d:

if [ ! -d $FILE];
    echo "The file $FILE is not a directory."

For more information, see section "6.4 Bash Conditional Expressions" of the bash documentation:

bash: working with files

See below for a series of wiki pages about working with files within a bash script.

bash: iterate over files in directory

Here is a sample code:


for file in $FILES
  # Do something with your file $file
  cat $f

Bash: write (echo) to stderr

3.6.4 Redirecting Standard Output and Standard Error

# Both are OK:
echo "WARNING! My error message." 1&>2
1&>2 echo "WARNING! My error message."

Note that &>2 is the same as 1&>2 since 1 is stdout and is the default output channel.

Or you could define a function:

print_error() {
   echo "$@" 1>&2;

print_error "WARNING! My error message."

Syndicate content