Find the answer to your Linux question:
Results 1 to 2 of 2
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    BASH Ternary Operator?

    Good afternoon!

    I had a question regarding the ternary operator. Does one exist for BASH? I know for Perl it exists.
    The code I'm trying to execute is:

    echo -n "Enter your name: "
    read NAME
    if [ -z "$NAME" ] ; then
    echo "You did NOT enter your name!"
    echo "Hello $NAME"

    (So basically, if the user enters a name, it says "Hello User!" else it tells them they did not enter their name). How would I simplify something like that into a ternary operator. Examples I've looked at on the web have not worked.



  2. #2
    Linux Engineer
    Join Date
    Jan 2005
    Saint Paul, MN
    read -p "Enter your name: " NAME
    [ -z "${NAME}" ] && echo You did NOT enter your name!" || echo "Hello ${NAME}"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts