On Sun, Oct 20, 2019 at 10:21:20PM -0400, Ken Heard wrote: > #! /bin/bash > CURPWD=$PWD > cd /home/ken > tar -czf /media/fde/backups/kfinancescurrent.tgz --wildcards\ > - --exclude-from=docs/tarlists/kfinancesarchive.lst docs/01-kens/Finances > cd $CURPWD
What happens if cd /home/ken fails? Suddenly your tar command is running from the wrong location. You must always check the result of a cd command, and take appropriate action when it fails. For example, #!/bin/bash cd /home/ken || exit tar czf ... If this is the entire script, then that's all you need. You don't need the second cd command at all, because the current working directory of the script doesn't matter after the script exits. If there's other stuff after this tar command, and you want to "undo" the cd that you used for tar, there are a few good ways to go about this. The first is to encapsulate the cd+tar into a subshell. The cd happens in the subshell, so as soon as the subshell terminates, the original process resumes with its original working directory. #!/bin/bash (cd /home/ken && tar czf ... ) other commands The second is to take advantage of tar's -C option, which allows tar to change directory internally. Then, your script doesn't need to cd at all. #!/bin/bash tar c -C /home/ken -zf ... Finally, if you *do* choose to store a directory name in a variable and use that variable later in the script, you must quote it correctly. cd "$olddir" This is not optional, and not negotiable. The quotes are REQUIRED. https://mywiki.wooledge.org/Quotes