762

How can I determine the name of the Bash script file inside the script itself?

Like if my script is in file runme.sh, then how would I make it to display "You are running runme.sh" message without hardcoding that?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Ma99uS
  • 10,605
  • 8
  • 32
  • 42
  • 3
    Similar [Can a bash script tell what directory its stored in?](to http://stackoverflow.com/questions/59895/can-a-bash-script-tell-what-directory-its-stored-in) – Rodrigue Jul 05 '12 at 08:22
  • 1
    For directory, see: [Getting the source directory of a Bash script from within](https://stackoverflow.com/q/59895/55075). – kenorb Jul 19 '17 at 18:39

26 Answers26

783
me=$(basename "$0")

For reading through a symlink1, which is usually not what you want (you usually don't want to confuse the user this way), try:

me="$(basename "$(test -L "$0" && readlink "$0" || echo "$0")")"

IMO, that'll produce confusing output. "I ran foo.sh, but it's saying I'm running bar.sh!? Must be a bug!" Besides, one of the purposes of having differently-named symlinks is to provide different functionality based on the name it's called as (think gzip and gunzip on some platforms).


1 That is, to resolve symlinks such that when the user executes foo.sh which is actually a symlink to bar.sh, you wish to use the resolved name bar.sh rather than foo.sh.

alper
  • 2,919
  • 9
  • 53
  • 102
Tanktalus
  • 21,664
  • 5
  • 41
  • 68
  • 52
    $0 gives you the name via which the script was invoked, not the real path of the actual script file. – Chris Conway Oct 10 '08 at 17:48
  • 6
    It works unless you're being called via symlink. But, even then, it's usually what you want anyway, IME. – Tanktalus Oct 10 '08 at 17:50
  • You can be called by a name that doesn't exist as a file at all -- most common example, calling sh as "-/bin/sh" causes it to act as a login shell. See `exec -a` in Bash, or `execvp("/bin/foo", {"blah", ...})`. – ephemient Oct 10 '08 at 18:48
  • Yes, that's a better example. – Chris Conway Oct 10 '08 at 21:06
  • If your script changes its behavior based on how its called, its a good idea to ensure some catch all when evaluating $0 .. otherwise, a simple symlink could make it do odd things :) I.e. if the link was named foobar and the script only accounts for foo or bar. – Tim Post Mar 12 '09 at 17:03
  • 97
    Doesn't work for scripts which are sourced as opposed to invoked. – Charles Duffy Jan 27 '11 at 18:42
  • 1
    @Charles Duffy: See [Dimitre Radoulov's answer below](http://stackoverflow.com/questions/192319/in-the-bash-script-how-do-i-know-the-script-file-name/639500#639500). – Serge Wautier Oct 27 '11 at 08:14
  • 9
    -1, 1. readlink will only travel one symlink deep, 2. `$0` in the first example is subject to word splitting, 3. `$0` is passed to basename, readlink, and echo in a position which allows it to be treated as a command line switch. I suggest instead `me=$(basename -- "$0")` or much more efficiently at the expense of readability, `me=${0##*/}`. For symlinks, `me=$(basename -- "$(readlink -f -- "$0")")` assuming gnu utils, otherwise it will be a very long script which I will not write here. – Score_Under Apr 28 '15 at 17:22
  • @Score_Under why complain about this stuff instead of improving the answer? – BUFU Aug 12 '21 at 21:31
  • i don't know why this solution has many up votes as it is flawless.... as relies on FOUR FOREIGN commands/programs like: readlink echo basename test which may not be available for use in that environment (so i'm dwn v-ing) – ceph3us Mar 05 '22 at 20:17
312
# ------------- SCRIPT ------------- #

#!/bin/bash

echo
echo "# arguments called with ---->  ${@}     "
echo "# \$1 ---------------------->  $1       "
echo "# \$2 ---------------------->  $2       "
echo "# path to me --------------->  ${0}     "
echo "# parent path -------------->  ${0%/*}  "
echo "# my name ------------------>  ${0##*/} "
echo
exit

# ------------- CALLED ------------- #

# Notice on the next line, the first argument is called within double, 
# and single quotes, since it contains two words

$  /misc/shell_scripts/check_root/show_parms.sh "'hello there'" "'william'"

# ------------- RESULTS ------------- #

# arguments called with --->  'hello there' 'william'
# $1 ---------------------->  'hello there'
# $2 ---------------------->  'william'
# path to me -------------->  /misc/shell_scripts/check_root/show_parms.sh
# parent path ------------->  /misc/shell_scripts/check_root
# my name ----------------->  show_parms.sh

# ------------- END ------------- #
Bill Hernandez
  • 3,129
  • 1
  • 15
  • 5
  • 11
    @NickC see [**Substring Removal**](http://tldp.org/LDP/abs/html/string-manipulation.html) – cychoi Apr 22 '14 at 17:00
  • 1
    How do I get just `show_params`, i.e. the name without any optional extension? – Asclepius Jul 08 '15 at 01:54
  • Not working in case the script is invoked from another folder. The path is included in the `${0##*/}`. Tested using GitBash. – AlikElzin-kilaka Jul 30 '15 at 06:30
  • 1
    The above didn't work for me from a .bash_login script, but the Dimitre Radoulov solution of $BASH_SOURCE works great. – John May 06 '16 at 19:11
  • @John Surely you mean *.bash_profile*? Or alternatively *.bashrc*? You should know though that there are some subtle differences in how they're invoked/sourced. I’m dead tired but if I’m thinking right that would be quite likely the problem. One place it does matter is if you're logging into a system remotely over e.g. ssh versus at the local system. – Pryftan Jul 22 '19 at 19:43
  • @Acumenus `echo "# my name ------------------> $(cut -d . -f 1 <<<${0##*/}) "` – poboxy Apr 18 '20 at 19:29
  • 1
    @AlikElzin-kilaka For me, and 5 years later, and with native bash 5.0-4, it works, even when the script is called from another folder. – Binarus May 11 '21 at 17:56
  • How do I *always* get the full parent path? For example, if `$PWD` is `/misc/shell_scripts/` and I run `$ check_root/show_parms.sh` `${0%/*}` returns `check_root` not `/misc/shell_scripts/check_root`... – nooblag May 12 '21 at 19:53
  • The first parameter need only one kind of quotation marks and the second doesn't need quotation at all (script code above suggest it is necessary to use double and single quotes). – Claudio Dec 20 '22 at 05:12
228

With bash >= 3 the following works:

$ ./s
0 is: ./s
BASH_SOURCE is: ./s
$ . ./s
0 is: bash
BASH_SOURCE is: ./s

$ cat s
#!/bin/bash

printf '$0 is: %s\n$BASH_SOURCE is: %s\n' "$0" "$BASH_SOURCE"
user
  • 5,335
  • 7
  • 47
  • 63
Dimitre Radoulov
  • 27,252
  • 4
  • 40
  • 48
  • 25
    Great! That's the answer which works for both ./scrip.sh and source ./script.sh – zhaorufei Oct 11 '10 at 07:08
  • 7
    This is what I want, and it is easily to use "`dirname $BASE_SOURCE`" to get directory that the scripts located. – Larry Cai Sep 06 '11 at 04:43
  • 2
    I almost learnt that difference the hard way when writing a self-deleting script. Luckily 'rm' was aliased to 'rm -i' :) – kervin May 04 '12 at 21:47
  • anyway to the . ./s to get the name ./s instead of bash? I've found that $1 is not always set to ./s... – David Mokon Bond Feb 12 '13 at 14:14
  • 1
    It's in *BASH_SOURCE*, isn't it? – Dimitre Radoulov Feb 12 '13 at 15:10
  • `BASH_SOURCE` is an array and shouldn't be used like this. – styrofoam fly Jul 04 '18 at 18:11
  • @kervin Careful with that one... It really amounts to babysitting and what happens if you forget to set it up in a system that doesn't have it by default? I personally disable it on all my systems. It's a bloody nuisance and the other *-I* (interactive once) is not much better for me. Besides if I say I want to delete something then I don't want to be asked if I’m sure of it. Of course I’m sure of it! And even if I type a path wrong due to a typo say there is this thing called 'backing up'. Anyway the point is that there is a chance it's not on all systems and could be disastrous. – Pryftan Jul 22 '19 at 19:46
  • @Pryftan any alternatives then? – DarkTrick Oct 11 '20 at 22:50
  • @Pryftan aliasing rm to rm -i *is* being careful. If I am 100% sure, I can always use \rm or command rm... and when I'm on a sytem that is new to me, I have to adapt either way. – BUFU Aug 12 '21 at 21:26
125

$BASH_SOURCE gives the correct answer when sourcing the script.

This however includes the path so to get the scripts filename only, use:

$(basename $BASH_SOURCE) 
kenorb
  • 155,785
  • 88
  • 678
  • 743
Zainka
  • 1,259
  • 1
  • 8
  • 2
  • 7
    This answer is IMHO the best one because teh solution makes use of self-documenting code. $BASH_SOURCE is totally understandable without reading any documentation whereas e.g. ${0##*/} is not – Andreas M. Oberheim Sep 06 '18 at 12:28
  • 1
    This answer is of more value i belief because if we run like `. [arguments]`, `$0` would give name of caller shell. Well at least on OSX for sure. – Mihir Luthra Jul 15 '19 at 17:48
  • 3
    @AndreasM.Oberheim the drawback of `basename` is that a separate process has to be forked, whereas `##*/` is just bash's own processing. You can still use `$BASH_SOURCE` with it, though: `${BASH_SOURCE[0]##*/}` – mo. Oct 17 '21 at 02:40
72

If the script name has spaces in it, a more robust way is to use "$0" or "$(basename "$0")" - or on MacOS: "$(basename \"$0\")". This prevents the name from getting mangled or interpreted in any way. In general, it is good practice to always double-quote variable names in the shell.

Andrew Faulkner
  • 3,662
  • 3
  • 21
  • 24
Josh Lee
  • 171,072
  • 38
  • 269
  • 275
34

If you want it without the path then you would use ${0##*/}

Mr. Muskrat
  • 22,772
  • 3
  • 20
  • 21
  • And what if I want it without any optional file extension? – Asclepius Jul 08 '15 at 01:56
  • To remove an extension, you can try "${VARIABLE%.ext}" where VARIABLE is the value you got from ${0##*/} and ".ext" is the extension you want to remove. – BrianV Apr 19 '19 at 15:31
23

To answer Chris Conway, on Linux (at least) you would do this:

echo $(basename $(readlink -nf $0))

readlink prints out the value of a symbolic link. If it isn't a symbolic link, it prints the file name. -n tells it to not print a newline. -f tells it to follow the link completely (if a symbolic link was a link to another link, it would resolve that one as well).

Community
  • 1
  • 1
Travis B. Hartwell
  • 3,124
  • 21
  • 11
20

I've found this line to always work, regardless of whether the file is being sourced or run as a script.

echo "${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}"

If you want to follow symlinks use readlink on the path you get above, recursively or non-recursively.

The reason the one-liner works is explained by the use of the BASH_SOURCE environment variable and its associate FUNCNAME.

BASH_SOURCE

An array variable whose members are the source filenames where the corresponding shell function names in the FUNCNAME array variable are defined. The shell function ${FUNCNAME[$i]} is defined in the file ${BASH_SOURCE[$i]} and called from ${BASH_SOURCE[$i+1]}.

FUNCNAME

An array variable containing the names of all shell functions currently in the execution call stack. The element with index 0 is the name of any currently-executing shell function. The bottom-most element (the one with the highest index) is "main". This variable exists only when a shell function is executing. Assignments to FUNCNAME have no effect and return an error status. If FUNCNAME is unset, it loses its special properties, even if it is subsequently reset.

This variable can be used with BASH_LINENO and BASH_SOURCE. Each element of FUNCNAME has corresponding elements in BASH_LINENO and BASH_SOURCE to describe the call stack. For instance, ${FUNCNAME[$i]} was called from the file ${BASH_SOURCE[$i+1]} at line number ${BASH_LINENO[$i]}. The caller builtin displays the current call stack using this information.

[Source: Bash manual]

gkb0986
  • 3,099
  • 1
  • 24
  • 22
  • 2
    It works if you source in file `a` (assume `a`'s contents is `echo "${BASH_SOURCE[${#BASH_SOURCE[@]} - 1]}"`) from an interactive session -- then it will give you `a`'s path. But if you write a script `b` with `source a` in it and run `./b`, it'll return `b`'s path. – Petr Skocik Jan 30 '15 at 12:59
  • Doesn't your answer give the FIRST file rather than the last/most current file? I'm finding that ${BASH_SOURCE[0]}" or just $BASH_SOURCE tells me the current file - i.e. the answer @Zainka posted. – rich p Jun 19 '20 at 16:13
20

Since some comments asked about the filename without extension, here's an example how to accomplish that:

FileName=${0##*/}
FileNameWithoutExtension=${FileName%.*}

Enjoy!

Simon Mattes
  • 4,866
  • 2
  • 33
  • 53
12

These answers are correct for the cases they state but there is a still a problem if you run the script from another script using the 'source' keyword (so that it runs in the same shell). In this case, you get the $0 of the calling script. And in this case, I don't think it is possible to get the name of the script itself.

This is an edge case and should not be taken TOO seriously. If you run the script from another script directly (without 'source'), using $0 will work.

Jim Dodd
  • 241
  • 2
  • 3
  • 2
    You have a very good point. Not an edge case IMO. There is a solution though: See [Dimitre Radoulov's answer above](http://stackoverflow.com/questions/192319/in-the-bash-script-how-do-i-know-the-script-file-name/639500#639500) – Serge Wautier Oct 27 '11 at 08:12
11

Re: Tanktalus's (accepted) answer above, a slightly cleaner way is to use:

me=$(readlink --canonicalize --no-newline $0)

If your script has been sourced from another bash script, you can use:

me=$(readlink --canonicalize --no-newline $BASH_SOURCE)

I agree that it would be confusing to dereference symlinks if your objective is to provide feedback to the user, but there are occasions when you do need to get the canonical name to a script or other file, and this is the best way, imo.

simon
  • 15,344
  • 5
  • 45
  • 67
10
this="$(dirname "$(realpath "$BASH_SOURCE")")"

This resolves symbolic links (realpath does that), handles spaces (double quotes do this), and will find the current script name even when sourced (. ./myscript) or called by other scripts ($BASH_SOURCE handles that). After all that, it is good to save this in a environment variable for re-use or for easy copy elsewhere (this=)...

jcalfee314
  • 4,642
  • 8
  • 43
  • 75
  • 4
    FYI `realpath` is not a built-in BASH command. It is a standalone executable that is available only in certain distributions – StvnW Jul 04 '14 at 13:59
  • Can confirm that, in my 14.04 box it's not available and I had to use `readlink` instead – Liso Nov 15 '21 at 01:31
10

This works fine with ./self.sh, ~/self.sh, source self.sh, source ~/self.sh:

#!/usr/bin/env bash

self=$(readlink -f "${BASH_SOURCE[0]}")
basename=$(basename "$self")

echo "$self"
echo "$basename"

Credits: I combined multiple answers to get this one.

Nishant
  • 20,354
  • 18
  • 69
  • 101
  • 2
    `$(readlink -f "${BASH_SOURCE[0]}")` only works if you are using bash. `$(readlink -f "${BASH_SOURCE:-$0}")` works regardless. – 5p0ng3b0b Dec 02 '21 at 16:17
9

You can use $0 to determine your script name (with full path) - to get the script name only you can trim that variable with

basename $0
VolkA
  • 34,983
  • 7
  • 37
  • 37
8

if your invoke shell script like

/home/mike/runme.sh

$0 is full name

 /home/mike/runme.sh

basename $0 will get the base file name

 runme.sh

and you need to put this basic name into a variable like

filename=$(basename $0)

and add your additional text

echo "You are running $filename"

so your scripts like

/home/mike/runme.sh
#!/bin/bash 
filename=$(basename $0)
echo "You are running $filename"
LawrenceLi
  • 407
  • 5
  • 9
2
echo "$(basename "`test -L ${BASH_SOURCE[0]} \
                   && readlink ${BASH_SOURCE[0]} \
                   || echo ${BASH_SOURCE[0]}`")"
ecwpz91
  • 1,527
  • 1
  • 13
  • 8
2

In bash you can get the script file name using $0. Generally $1, $2 etc are to access CLI arguments. Similarly $0 is to access the name which triggers the script(script file name).

#!/bin/bash
echo "You are running $0"
...
...

If you invoke the script with path like /path/to/script.sh then $0 also will give the filename with path. In that case need to use $(basename $0) to get only script file name.

rashok
  • 12,790
  • 16
  • 88
  • 100
2

Short, clear and simple, in my_script.sh

#!/bin/bash

running_file_name=$(basename "$0")

echo "You are running '$running_file_name' file."

Out put:

./my_script.sh
You are running 'my_script.sh' file.
1

Info thanks to Bill Hernandez. I added some preferences I'm adopting.

#!/bin/bash
function Usage(){
    echo " Usage: show_parameters [ arg1 ][ arg2 ]"
}
[[ ${#2} -eq 0 ]] && Usage || {
    echo
    echo "# arguments called with ---->  ${@}     "
    echo "# \$1 ----------------------->  $1       "
    echo "# \$2 ----------------------->  $2       "
    echo "# path to me --------------->  ${0}     " | sed "s/$USER/\$USER/g"
    echo "# parent path -------------->  ${0%/*}  " | sed "s/$USER/\$USER/g"
    echo "# my name ------------------>  ${0##*/} "
    echo
}

Cheers

linxuser
  • 27
  • 1
1

To get the "realpath" of script or sourced scripts in all cases :

fullname=$(readlink $0)  # Take care of  symbolic links
dirname=${fullname%/*}       # Get (most of the time) the dirname
realpath=$(dirname $BASH_SOURCE) # TO handle sourced scripts
[ "$realpath" = '.' ] && realpath=${dirname:-.}

Here is the bash script to generate (in a newly created "workdir" subdir and in "mytest" in current dir), a bash script which in turn will source another script, which in turm will call a bash defined function .... tested with many ways to launch them :

#!/bin/bash
##############################################################

ret=0

fullname=$(readlink $0)  # Take care of  symbolic links
dirname=${fullname%/*}       # Get (most of the time) the dirname
realpath=$(dirname $BASH_SOURCE) # TO handle sourced scripts
[ "$realpath" = '.' ] && realpath=${dirname:-.}

fullname_withoutextension=${fullname%.*}

mkdir -p workdir
cat <<'EOD' > workdir/_script_.sh
#!/bin/bash
##############################################################

ret=0

fullname=$(readlink $0)  # Take care of  symbolic links
dirname=${fullname%/*}       # Get (most of the time) the dirname
realpath=$(dirname $BASH_SOURCE) # TO handle sourced scripts
[ "$realpath" = '.' ] && realpath=${dirname:-.}

fullname_withoutextension=${fullname%.*}

echo
echo "# ------------- RESULTS ------------- #"
echo "# path to me (\$0)----------->  ${0}     "
echo "# arguments called with ---->  ${@}     "
echo "# \$1 ----------------------->  $1       "
echo "# \$2 ----------------------->  $2       "
echo "# path to me (\$fullname)---->  ${fullname} "
echo "# parent path(\${0%/*})------>  ${0%/*}  "
echo "# parent path(\$dirname)----->  ${dirname} "
echo "# my name ----\${0##*/}------>  ${0##*/} "
echo "# my source -\${BASH_SOURCE}->  ${BASH_SOURCE} "
echo "# parent path(from BASH_SOURCE) -> $(dirname $BASH_SOURCE)"
echo "# my function name -\${FUNCNAME[0]}------>  ${FUNCNAME[0]}"
echo "# my source or script real path (realpath)------------------>  $realpath"
echo
[ "$realpath" = "workdir" ] || ret=1
[ $ret = 0 ] || echo "*******************************************************"
[ $ret = 0 ] || echo "***********   ERROR  **********************************"
[ $ret = 0 ] || echo "*******************************************************"

show_params () {
        echo
        echo "# --- RESULTS FROM show_params() ---- #"
        echo "# path to me (\$0)----------->  ${0}     "
        echo "# arguments called with ---->  ${@}     "
        echo "# \$1 ----------------------->  $1       "
        echo "# \$2 ----------------------->  $2       "
        echo "# path to me (\$fullname)---->  ${fullname} "
        echo "# parent path(\${0%/*})------>  ${0%/*}  "
        echo "# parent path(\$dirname)----->  ${dirname} "
        echo "# my name ----\${0##*/}------>  ${0##*/} "
        echo "# my source -\${BASH_SOURCE}->  ${BASH_SOURCE} "
        echo "# parent path(from BASH_SOURCE) -> $(dirname $BASH_SOURCE)"
        echo "# my function name -\${FUNCNAME[0]}------>  ${FUNCNAME[0]}"
        echo "# my source or script real path (realpath)------------------>  $realpath"
        echo
        [ "$realpath" = "workdir" ] || ret=1
        [ $ret = 0 ] || echo "*******************************************************"
        [ $ret = 0 ] || echo "***********   ERROR  **********************************"
        [ $ret = 0 ] || echo "*******************************************************"

}
show_params "$@"

EOD

cat workdir/_script_.sh > workdir/_side_by_side_script_sourced.inc

cat <<'EOD' >> workdir/_script_.sh

echo "# . $realpath/_side_by_side_script_sourced.inc 'hello there' 'william'"
. $realpath/_side_by_side_script_sourced.inc 'hello there' 'william'

[ $ret = 0 ] || echo "*******************************************************"
[ $ret = 0 ] || echo "***********   ERROR  **********************************"
[ $ret = 0 ] || echo "*******************************************************"
EOD

chmod +x  workdir/_script_.sh
[ -L _mytest_ ] && rm _mytest_
ln -s workdir/_script_.sh _mytest_

# ------------- CALLED ------------- #

called_by () {
        echo '=========================================================================='
        echo " Called by : " "$@"
        echo '=========================================================================='
        eval "$@"
}

called_by bash _mytest_
called_by ./_mytest_

called_by bash workdir/_script_.sh
called_by workdir/_script_.sh
called_by . workdir/_script_.sh


# ------------- RESULTS ------------- #

echo
echo
[ $ret = 0 ] || echo "*******************************************************"
[ $ret = 0 ] || echo "***********   ERROR  **********************************"
[ $ret = 0 ] || echo "*******************************************************"
echo
[ $ret = 0 ] && echo ".... location of scripts (\$realpath) should always be equal to $realpath, for all test cases at date".
echo

# ------------- END ------------- #
0

Here is what I came up with, inspired by Dimitre Radoulov's answer (which I upvoted, by the way).

script="$BASH_SOURCE"
[ -z "$BASH_SOURCE" ] && script="$0"

echo "Called $script with $# argument(s)"

regardless of the way you call your script

. path/to/script.sh

or

./path/to/script.sh
Salathiel Genese
  • 1,639
  • 2
  • 21
  • 37
0
DIRECTORY=$(cd `dirname $0` && pwd)

I got the above from another Stack Overflow question, Can a Bash script tell what directory it's stored in?, but I think it's useful for this topic as well.

Community
  • 1
  • 1
Koter84
  • 47
  • 3
0

$0 will give the name of the script you are running. Create a script file and add following code

#!/bin/bash
echo "Name of the file is $0"

then run from terminal like this

./file_name.sh
Ali Yar Khan
  • 1,231
  • 2
  • 11
  • 33
0

As an option not listed in above answers you can use script's pid to get a lot of info including name from /proc/$pid folder, like this:

#!/bin/bash

pid=$$ # get script's pid to var $pid

echo $pid

# there is a file in /proc/$pid folder called cmdline
# this file stores full command line used by script
cat /proc/$pid/cmdline

output:

$ ./test 1 '2 3' 4 
12924
/bin/bash./test12 34

the output looks a bit messy it's just one string... not exactly it actually has delimiters but to be able to see them we need to change our cat command like this:

#!/bin/bash

pid=$$ # get script's pid to var $pid

echo $pid

# add -A - show all option for cat
cat -A /proc/$pid/cmdline

there ya go, now we can see delimeters^@:

$ ./test 1 '2 3' 4 
13002
/bin/bash^@./test^@1^@2 3^@4^@

lets add some more hacking to get the name:

#!/bin/bash

pid=$$ # get script's pid to var $pid

# tr to change '^@' into ' ' and awk to print 2nt column(script name)
name=$(cat -A /proc/$pid/cmdline | tr '^@' ' ' | awk '{print $2}')

echo "
pid:  $pid
name: $name
"

result:

$ ./test 1 '2 3' 4 

pid:  13091
name: ./test
Ivan
  • 6,188
  • 1
  • 16
  • 23
-2

echo "You are running $0"

mmacaulay
  • 3,049
  • 23
  • 27
-8

somthing like this?

export LC_ALL=en_US.UTF-8
#!/bin/bash
#!/bin/sh

#----------------------------------------------------------------------
start_trash(){
ver="htrash.sh v0.0.4"
$TRASH_DIR  # url to trash $MY_USER
$TRASH_SIZE # Show Trash Folder Size

echo "Would you like to empty Trash  [y/n]?"
read ans
if [ $ans = y -o $ans = Y -o $ans = yes -o $ans = Yes -o $ans = YES ]
then
echo "'yes'"
cd $TRASH_DIR && $EMPTY_TRASH
fi
if [ $ans = n -o $ans = N -o $ans = no -o $ans = No -o $ans = NO ]
then
echo "'no'"
fi
 return $TRUE
} 
#-----------------------------------------------------------------------

start_help(){
echo "HELP COMMANDS-----------------------------"
echo "htest www                 open a homepage "
echo "htest trash               empty trash     "
 return $TRUE
} #end Help
#-----------------------------------------------#

homepage=""

return $TRUE
} #end cpdebtemp

# -Case start
# if no command line arg given
# set val to Unknown
if [ -z $1 ]
then
  val="*** Unknown  ***"
elif [ -n $1 ]
then
# otherwise make first arg as val
  val=$1
fi
# use case statement to make decision for rental
case $val in
   "trash") start_trash ;;
   "help") start_help ;;
   "www") firefox $homepage ;;
   *) echo "Sorry, I can not get a $val   for you!";;
esac
# Case stop
hynt
  • 1
  • 2
  • 5
    -1, does not answer the question (doesn't show how to find the script's name), and is a confusing example in a very buggy script. – Score_Under Apr 28 '15 at 17:32