Twitter Updates

    follow me on Twitter

    G-AVLN in front of her home

    G-AVLN in front of her home

    Mostly Unix and Linux topics. But flying might get a mention too.

    Wednesday, November 15, 2006

    Quoting command substitution

    When we learn the inner works of a shell, we quickly realise the benefit of using double quotes with request for variable substitution.

    For example, to assign a value to a variable (and preserve leading spaces), we would use:

    $ address=" 23 Acacia Avenue "

    A correct quotation is needed to recall the value complete with the spaces:

    $ echo "<$address>"

    The less known application of the quotes is when they are around the command substitution syntax. By default, POSIX shells remove trailing new-lines during command substitution. Double quotes change this behaviour!

    Compare the output of the following commands:

    $ echo $(ls)
    log.1 log.2 log.3

    and

    $ echo "$(ls)"
    log.1
    log.2
    log.3

    No comments:

    Blog Archive