Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux Shell script what dirname and ? means?

Tags:

linux

bash

unix

Can any body tell me what this command means?

selfDir=$(cd "$(dirname "$0")"; pwd) ?

I know

  • $0 = running script name
  • pwd = current working dir
  • cd = to change dir

What I need is what is dirname? and what ? means at end to make this line completely understandable.

like image 876
Elvin Avatar asked Dec 05 '13 03:12

Elvin


People also ask

What is dirname in shell script?

dirname is a standard computer program on Unix and Unix-like operating systems. When dirname is given a pathname, it will delete any suffix beginning with the last slash ( '/' ) character and return the result. dirname is described in the Single UNIX Specification and is primarily used in shell scripts. dirname.

What does dirname $0 mean?

dirname $0 takes a filename (in this case, $0 or the path where the shell found that file), and echo es the directory that it is stored in.

What is dirname and basename?

The functions dirname() and basename() break a null-terminated pathname string into directory and filename components. In the usual case, dirname() returns the string up to, but not including, the final '/', and basename() returns the component following the final '/'.

What is $# $? $@ Etc in Linux shell script?

$* Stores all the arguments that were entered on the command line ($1 $2 ...). "$@" Stores all the arguments that were entered on the command line, individually quoted ("$1" "$2" ...). So basically, $# is a number of arguments given when your script was executed. $* is a string containing all arguments.


1 Answers

The dirname command removes the trailing / component from the NAME and prints the remaining portion. If the NAME does not contain / component then it prints '.' (means current directory)

Dirname Command Example:

Remove the file name from absolute path.

Let say my directory path is /usr/local/bin/add.sh. Now i want to remove /add.sh and display only /usr/local/bin, then we can use the dirname command.

dirname /usr/local/bin/add.sh
/usr/local/bin

NAME

dirname - strip non-directory suffix from file name

SYNOPSIS

dirname NAME 
dirname OPTION 

DESCRIPTION

Print NAME with its trailing /component removed; if NAME contains no /’s, output ‘.’ (meaning the current directory).

Edit: Also, Some characters have special functions in linux commands ? <-- Matches one character

Source

like image 94
Vinayak Pahalwan Avatar answered Sep 27 '22 16:09

Vinayak Pahalwan