Show commands without executing them

后端 未结 4 699
太阳男子
太阳男子 2020-12-08 19:55

I often interactively loop over e.g. my files and want to perform a specific operation on all of them, let\'s say I\'d like to rename all files:

for file in          


        
4条回答
  •  庸人自扰
    2020-12-08 20:03

    There is no option for "dry run" as explained by devnull but there is a simple workaround:

    debug=
    #debug=echo
    
    $debug mv "$file" "${file}_new"
    

    If you remove the comment from the second assignment (without changing anything else), you enable "dry run" for the dangerous mv command.

    A more elaborate approach would be to check some condition (like a command line option):

    debug=
    if [[ ...enable dry run?... ]]; then
        debug=echo
    fi
    

    Note: The empty assignment is only necessary when you have the option -u ("Treat unset variables as an error when substituting.") enabled.

    Important: This won't work well, when your commands use redirections (because the shell will always do them before the command is even started).

提交回复
热议问题