What is the best way of supporting in the script prompts and arguments at the same time? I want to show prompts if arguments weren't provided.

Is there something better/shorter than this? ⇩


if [ -z "${PROJECT_DIR}" ]; then
    echo "Directory where project resides:"
    read PROJECT_DIR

if [ -z "${SITE_NAME}" ]; then
    echo "Name of the website:"
    read SITE_NAME

if [ -z "${ADMIN_PWD}" ]; then
    echo "Admin password:"
    read ADMIN_PWD

if [ -z "${THEME_DIR}" ]; then
    echo "Directory of the theme:"
    read THEME_DIR

if [ -z "${THEME_NAME}" ]; then
    echo "Name of the theme:"
    read THEME_NAME
  • 1
    At least for the directories it seems pretty stupid to have a prompt - when you supply the path as an argument, you can make use of the shell's tab completion. I'd prefer the script to die if the directories weren't supplied as arguments instead if prompting for them. Jan 23, 2014 at 23:15
  • 1
    Also note that supplying passwords as arguments is unsafe. Every user can snoop on the arguments of your script while it is running: stackoverflow.com/questions/6607675/… Jan 23, 2014 at 23:17
  • You are right, this is only stupid example :) Jan 23, 2014 at 23:17

2 Answers 2


You can shorten it a bit by using a function:


  declare -g $1="$2"
  if [ -z "${!1}" ]; then
    echo "$3"
    read $1

ask PROJECT_DIR "$1" "Directory where project resides:"
ask SITE_NAME   "$2" "Name of the website:"
ask ADMIN_PWD   "$3" "Admin password:"
ask THEME_DIR   "$4" "Directory of the theme:"
ask THEME_NAME  "$5" "Name of the theme:"


This requires bash though and won't work in sh.

  • It actually requires bash > 4.2 for declare -g. It can be rewritten to not use it though.
    – jordanm
    Jan 23, 2014 at 23:51

You can do so with parameter expansion using ${variable:-default}

For example:

function get_value_as_arg_or_prompt() {
  value=${1:-$(read -p "Enter: " x && echo "$x")}
  echo "$value"

If giving an argument:

$ get_value_as_arg_or_prompt "Hi there"
Hi there

If not giving an argument:

$ get_value_as_arg_or_prompt
Enter: I'm here
I'm here
  • Note that without quoting $xor $value when you use them with echo, you will get the wrong results if the string entered by the user is something like *. Also, the shell would compress multiple consecutive spaces into single spaces, and depending on shell options, echo may interpret backslashes (might be a common character in passwords). It's safer to use e.g. printf '%s\n' "$x". See unix.stackexchange.com/questions/65803 unix.stackexchange.com/questions/171346 unix.stackexchange.com/questions/68694
    – Kusalananda
    Apr 13, 2020 at 14:13
  • @Kusalananda right, the echo should rather be quoted. But I did not need to use printf '%s\n' "$x" to include special characters.
    – Noam Manos
    Apr 19, 2020 at 15:21
  • 1
    I dunno. Try entering -n or -e at the prompt.
    – Kusalananda
    Apr 19, 2020 at 16:41

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .