How do i use bash command substitution?



  • There are two ways to use command substitution in bash. One is using back ticks and the other is using $()

    For back ticks, a single for loop is a good example. This lists the contents of dir

    This expands the $PWD variable to the value of the variable.

    Effectively like doing:

    for i in `ls /home/myuser`
    
    for i in `ls $PWD`
    do
    echo $i
    done
    

    Using the $() notation for substitution. This will run the groups command and substitute the output in the for loop.

    Effectively like doing:

    for i in adm users samba devgrp
    
    for i in $(groups)
    do
    echo $i
    done
    

Log in to reply
 

© Lightnetics 2024