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
.
Related Posts:
- What is “-bash: !”: event not found”
- Why do you need to put #!/bin/bash at the beginning of a script file?
- “sed” command in bash
- How does “cat << EOF" work in bash?
- Pseudo-terminal will not be allocated because stdin is not a terminal
- Given two directory trees, how can I find out which files differ by content?
- Pseudo-terminal will not be allocated because stdin is not a terminal
- ./configure : /bin/sh^M : bad interpreter
- What is the difference between “#!/usr/bin/env bash” and “#!/usr/bin/bash”?
- Is there a “goto” statement in bash?
- Bash script: bad interpreter
- How to count lines in a document?
- What does `set -x` do?
- Writing a simple shell in C using fork/execvp
- chmod: changing permissions of ‘my_script.sh’: Operation not permitted
- What does set -e mean in a bash script?
- What does set -e mean in a bash script?
- How to add a timestamp to bash script log?
- Colors in bash after piping through less?
- How to run command as user who has /usr/sbin/nologin as Shell?
- How can I fully log all bash scripts actions?
- How do I deal with a filename that starts with the hyphen (-) character?
- Bash: No such file or directory?
- How do I split a string on a delimiter in Bash?
- “Couldn’t find a file descriptor referring to the console” on Ubuntu bash on Windows
- gzip: stdin: not in gzip format tar: Child returned status 1 tar: Error is not recoverable: exiting now
- How can I recursively find all files in current and subfolders based on wildcard matching?
- How to perform grep operation on all files in a directory?
- Pass a password to ssh in pure bash
- subprocess.Popen(): OSError: [Errno 8] Exec format error in python?
- How to substitute shell variables in complex text files
- -bash: syntax error near unexpected token `newline’ for display command
- How can I recursively find all files in current and subfolders based on wildcard matching?
- How do I use grep to search the current directory for all files having the a string “hello” yet display only .h and .cc files?
- Curl command for https ( SSL )
- How to exclude a directory in find . command
- How can I declare and use Boolean variables in a shell script?
- How to exclude a directory in find . command
- How can I declare and use Boolean variables in a shell script?
- Shell command to tar directory excluding certain files/folders
- Explaining the ‘find -mtime’ command
- “[ ]” vs. “[[ ]]” in Bash shell
- How do I split a string on a delimiter in Bash?
- How can I kill a process by name instead of PID, on Linux?
- Extract file basename without path and extension in bash
- mv: cannot stat error : No such file or directory error
- Diff command along with Grep gives “Binary file (standard input) matches”
- screen Cannot open your terminal ‘/dev/pts/0’ – please check
- Trying to use bash on Windows and got no installed distributions message
- -bash: fork: Cannot allocate memory
- Implementing shell in C and need help handling input/output redirection
- Command to change the default home directory of a user
- How to get the process ID to kill a nohup process?
- Implementation of multiple pipes in C
- What does “&” at the end of a linux command mean?
- tar: add all files and directories in current directory INCLUDING .svn and so on
- Syntax error near unexpected token ‘then’
- “find: paths must precede expression:” How do I specify a recursive search that also finds files in the current directory?
- Copy multiple files from one directory to another from Linux shell
- Shell script not running, command not found
- Bash script prints “Command Not Found” on empty lines
- How to use sed to extract substring
- Pipe to/from the clipboard in a Bash script
- Linux Bash: Move multiple different files into same directory
- How do I write stderr to a file while using “tee” with a pipe?
- How to include file in a bash shell script
- How to enable Bash in Windows 10 developer preview?
- What does ‘bash -c’ do?
- bash sh – command not found
- How can I sort du -h output by size
- What is the difference between double and single square brackets in bash?
- What are the functional differences between .profile .bash_profile and .bashrc
- How to run a command multiple times, using bash shell?
- Shell command to monitor changes in a file
- How do I prevent accidental rm -rf /*?
- How do I sleep for a millisecond in bash or ksh
- What does ‘set -e’ do, and why might it be considered dangerous?
- What’s the best way to check if a volume is mounted in a Bash script?
- Keeping a linux process running after I logout
- What useful things can one add to one’s .bashrc? [closed]
- How can I kill all stopped jobs?
- Linux command line best practices and tips?
- How to get pid of just started process
- Can you have more than one ~/.ssh/config file?
- SSH from A through B to C, using private key on B [closed]
- Is there a way to do a remote “ls” much like “scp” does a remote copy?
- How to apply a filter to real time output of `tail -f `?
- How to remove invalid characters from filenames?
- zcat/gzcat works in linux, not on osx. general linux/osx compatibility
- How do I join two named pipes into single input stream in linux
- How do I tell if a regular file does not exist in Bash?
- error: ‘Can’t connect to local MySQL server through socket ‘/var/run/mysqld/mysqld.sock’ (2)’ — Missing /var/run/mysqld/mysqld.sock
- How to mkdir only if a directory does not already exist?
- Opening a .tar.gz file with a single command
- How to make and apply SVN patch?
- How to check if an environment variable exists and get its value?
- Create a new file in git bash
- Can you Run Xcode in Linux?
- How to use dos2unix?
- How to add a default include path for GCC in Linux?