Bash Tips

  1. Q: How can I use the echo command so that output following the echo command appears on the same line?

    A: If you use the "-n" option with echo it won't send a newline at the end of its output.

    # echo "Test 1" ; echo "Test 2"
    Test 1
    Test 2
    # echo -n "Test 1" ; echo "Test 2"
    Test 1Test 2

  2. Q: How do I extract the filename from a variable containing the full path to the file and the filename?

    A: If you have a variable, let's call it somevar, containing a path and filename, e.g. /temp/somefile.txt, and just want the filename, you can strip away the path as follows:

    # somevar="/etc/mail/access"
    # echo ${somevar##*/}
    access

    This will strip away the longest match of any number of characters followed by a "/" and display the resultant string.

  3. Q: What string comparison operators are available?

    A: The following string comparison operators are available.

    OperatorTrue If
    string1 = string2astring1 matches string2
    sring1 != String2string1 doesn't match string2
    string1 < string2string1 is less than string2
    string1 > string2string1 is greater than string2
    -n string1string1 is not null, i.e. it has a length greater than 0
    -z string1string1 is null, i.e. it has a length of 0
    a. Note there is only one equal sign. This is a common source of errors

  4. Q: How do I remove blank lines from a file?

    A: You can use sed to remove blank lines from a file. The following command issued from a command line will do the trick for a file named temp.txt.

    sed -e '/^$/d' temp.txt

    In this case the output will be sent to standard output, e.g. your screen. If you want to create a new file without the blank lines, redirect the output as below. Don't try redirecting the output to the same file. You will just get an empty file if you try that, since the ">" will create a new empty file

    sed -e '/^$/d' temp.txt >newfile.txt

    If you want to use the command inside a script, you can use the following command.

    sed -e /^$/d "$1"

    The "-e" means an "editing" command follows (optional here). The "^" specifies the pattern to search for is at the beginning of the line and the "$" specifies the end of the line. In this case there are no characters in between the beginning and end of the line, i.e. a blank line. The slashes enclose the pattern you wish to work with, which is the blank line in this case and the "d" at the end tells sed to delete the blank lines. The arguments to a bash script begin with $1, e.g. if your script was called remove-blank-lines and you ran it with remove-blank-lines myfile.txt, then inside the bash script $1 represents the first argument, i.e. myfile.txt in this case. Putting it in double quotes allows whitespace and special characters to be part of the filename.

  5. Q: How do I recall previous commands entered in Bash?

    A: You can use the upward pointing arrow key on the keyboard to recall commands previously entered in Bash. To recall a specific command hit Ctrl-R and then start retyping the command you used previously. As you type, Bash will recall the last command entered that began with the letters you have typed so far. If you hit Ctrl-R again, it will search further back in the command history for the same text.

References:

  1. Manipulating Strings
  2. Shell Wrappers

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px

Valid HTML 4.01 Transitional

Created: July 29, 2005