Infrastructure Management Shell - Linux Netkiller ebook - Linux ebook

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?

Source: (StackOverflow)

Defining a variable with or without export

What is export for?

What is the difference between:

export name=value



Source: (StackOverflow)


how to use ssh to run shell script on a remote machine? [closed]

Could you please suggest me how to run a shell script on remote machine?

I have ssh configured on both machine A and B. My script is on machine A which will perform a task on machine B.

Source: (StackOverflow)

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.

Source: (StackOverflow)

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 -l *.php

That command works great within a given directory, but ignores subdirectories. I was thinking this might work, but it is returning 74, which is definitely not the case...

find . -name '*.php' | wc -l

What's the correct syntax to feed in all the files?

Source: (StackOverflow)

Check input argument in a Bash shell script

I need to check the existence of an input argument. I have the following script:

if [ "$1" -gt "-1" ]
  then echo hi

I get

[: : integer expression expected

How do I check the input argument1 first to see if it exists?

Source: (StackOverflow)

Get current directory name (without full path) in Bash Script

How would I get just the current working directory name in a bash script, or even better, just a terminal command.

pwd gives the full path of the current working directory, e.g. /opt/local/bin but I only want bin

Source: (StackOverflow)

How to reload .bash_profile from the command line?

How can I reload .bash_profile from the command line? I can get the shell to recognize changes to .bash_profile by exiting and logging back in but I would like to be able to do it on demand.

Source: (StackOverflow)

How do I prompt for input in a Linux shell script?

I want to pause input in a shell script, and prompt the user for choices. The standard 'Yes, No, or Cancel' type question. How do I accomplish this at a typical bash prompt?

Source: (StackOverflow)

How to scp a folder from remote to local?

I am not sure whether it is possible to scp a folder from remote to local, but still I am left with no other options. I use ssh to log into my server and from there I would like to copy the folder foo to home/user/Desktop (my local). Is there any command so that I can do this?

Source: (StackOverflow)

How can I replace a newline (\n) using sed?

How can I replace a newline (\n) using sed?

I unsuccesfully tried:

sed 's#\n# #g' file
sed 's#^$# #g' file

How to fix it?

Source: (StackOverflow)

How to mkdir only if a dir does not already exist?

I am writing a script to run under the KornShell (ksh) on AIX. I would like to use the mkdir command to create a directory. But the directory may already exist, in which case I do not want to do anything. So I want to either test to see that the directory does not exist, or suppress the "File exists" error that mkdir throws when it tries to create an existing directory.

Any thoughts on how best to do this?

Source: (StackOverflow)

Check if a directory exists in a shell script

What command can be used to check if a directory does or does not exist, within a shell script?

Source: (StackOverflow)

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:


So, if I want to use "head" on the output from g++, I can do something like this:

g++ lots_of_errors 2>&1 | head

so I can see only the first few errors.

I always have trouble remembering this, and I constantly have to go look it up, and it is mainly because I don't fully understand the syntax of this particular trick. Can someone break this up and explain character by character what "2>&1" means?

Source: (StackOverflow)

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:


Now I would like to split the strings by ; delimiter so that I have:


I don't necessarily need the ADDR1 and ADDR2 variables. If they are elements of an array that's even better.

After suggestions from the answers below, I ended up with the following which is what I was after:

#!/usr/bin/env bash


arr=$(echo $IN | tr ";" "\n")

for x in $arr
    echo "> [$x]"


> [bla@some.com]
> [john@home.com]

There was a solution involving setting Internal_field_separator (IFS) to ;. I am not sure what happened with that answer, how do you reset IFS back to default?

RE: IFS solution, I tried this and it works, I keep the old IFS and then restore it:


for x in $arr2
    echo "> [$x]"


BTW, when I tried


I only got the first string when printing it in loop, without brackets around $IN it works.

Source: (StackOverflow)