BASH Shell Frequently Asked Questions

This document contains a set of frequently-asked questions concerning Bash, the GNU Bourne-Again Shell. Bash is a free and default command interpreter with advanced features for both interactive use and shell programming under Linux. Also don’t forget TLDP bash guide / Advanced Bash-Scripting Guide.

🐧 If you liked this page, please support my work on Patreon or with a donation.
🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source & DevOps topics via:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04
2 comments… add one
  • Zhiyi Apr 16, 2008 @ 21:48

    See my solution

    #!/bin/bash

    #one approach is to use array
    dir=(/home/abc/xyz1 /home/abc/xyz2 /home/cft/dsf)
    x=0
    while [ $x -lt ${#dir[@]} ];
    do
    echo ${dir[x]}
    ((x++))
    done

    # another one is just use 'for'
    echo ""
    dirs="/home/abc/xyz1 /home/abc/xyz2 /home/cft/dsf"
    for dir in $dirs
    do
    echo $dir
    done

  • Manojg Mar 23, 2008 @ 16:04

    Hi,

    I am using bash shell. I have a list of selected directories and want to loop over these directories. The code is like this:

    =========================================
    #!/bin/bash
    dir1=/home/abc/xyz1
    dir2=/home/abc/xyz2
    dir3=/home/cft/dsf
    ————
    ————

    x=1
    dir=””
    while [ $x -le 8 ]; do
    echo $(“dir”$x)
    let x++
    done
    ===========================================

    In output it produces dir1, dir2, dir3 etc., not the full name directory as defined above. How should I do this?

    Thanks
    Manojg

Leave a Reply

Your email address will not be published. Required fields are marked *

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.