Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

bash shell script conditional assignment

Tags:

bash

shell

unix

I'm writing a bash shell script. There's a required first argument and I want to have an optional second argument.

If the second argument is omitted I want it to use the value of the first argument.

Currently I have:

SOMEVAR=${2:-Untitled}

How can I use something like basename $1 instead of Untitled?

like image 916
alfonso Avatar asked Jul 19 '11 09:07

alfonso


Video Answer


1 Answers

You can just do something like SOMEVAR=${2:-$(basename "$1")}. You can do any shell or variable in the optional part.

like image 75
norcalli Avatar answered Sep 18 '22 19:09

norcalli