Read a variable in bash with a default value

后端 未结 9 644
不思量自难忘°
不思量自难忘° 2020-11-29 16:13

I need to read a value from the terminal in a bash script. I would like to be able to provide a default value that the user can change.

# Please enter your          


        
9条回答
  •  情歌与酒
    2020-11-29 17:07

    Code:

    IN_PATH_DEFAULT="/tmp/input.txt"
    read -p "Please enter IN_PATH [$IN_PATH_DEFAULT]: " IN_PATH
    IN_PATH="${IN_PATH:-$IN_PATH_DEFAULT}"
    
    OUT_PATH_DEFAULT="/tmp/output.txt"
    read -p "Please enter OUT_PATH [$OUT_PATH_DEFAULT]: " OUT_PATH
    OUT_PATH="${OUT_PATH:-$OUT_PATH_DEFAULT}"
    
    echo "Input: $IN_PATH Output: $OUT_PATH"
    

    Sample run:

    Please enter IN_PATH [/tmp/input.txt]: 
    Please enter OUT_PATH [/tmp/output.txt]: ~/out.txt
    Input: /tmp/input.txt Output: ~/out.txt
    

提交回复
热议问题