Bash - Tilde (~) Expansion

Bash Liste Des Attaques Ovh

About

The tilde expansion replaces the tilde with a path value which is dependent of the syntax.

If the tilde expansion fails, the word is unchanged.

Syntax

If a word begins with an unquoted tilde character (~), all of the characters preceding the first unquoted slash (or all characters, if there is no unquoted slash) are considered a tilde-prefix.

~TildePrefix/

There is three forms of prefix:

  • A login name
  • A + or - (PWD or OLDPWD)
  • An integer with + or -. Ie an element in the directory stack.

Login Name

If none of the characters in the tilde-prefix are quoted, the characters in the tilde-prefix following the tilde are treated as a possible login name. If this login name is:

  • the null string, the tilde is replaced with the value of the shell parameter HOME. If HOME is unset, the home directory of the user executing the shell is substituted instead.
  • Otherwise, the tilde-prefix is replaced with the home directory associated with the specified login name.

If the login name is invalid, the word is unchanged.

Example:

  • Tilde with login name (deamon)
echo ~daemon/test
/usr/sbin/test

The HOME is defined in the file /etc/passwd

~[+-]

This expansion context permits to set:

If the tilde-prefix is a:

  • ~+, the value of the shell variable PWD replaces the tilde-prefix.
  • ~-, the value of the shell variable OLDPWD, if it is set, is substituted.

Example:

echo ~+ # PWD
echo ~- # OLDPWD

~N[+-]

If the characters following the tilde in the tilde-prefix consist of a number N, optionally prefixed by a ‘+’ or a ‘-’, the tilde-prefix is replaced with the corresponding element from the directory stack, as it would be displayed by the dirs builtin invoked with the tilde-prefix as an argument.

If the characters following the tilde in the tilde-prefix consist of a number without a leading + or -, + is assumed.

Example:

dirs
/root /

echo ~0
/root

echo ~1
/

Management

variable assignment

Each variable assignment is checked for unquoted tilde-prefixes immediately following a : or the first =. In these cases, tilde expansion is also performed.

Consequently, one may use file names with tildes in assignments to:

  • PATH,
  • MAILPATH,
  • and CDPATH,

and the shell assigns the expanded value.





Discover More
Bash Liste Des Attaques Ovh
Bash - (Builtin|Intern|System|Reserved|Shell) variable name

Reserved variable name are named that have a special meaning for the bash shell. PS1 defines the shell's command-line prompt. HOME defines the home directory for a user. PATH defines a list...
Bash Liste Des Attaques Ovh
Bash - (Environment) Variable

A variable is a parameters referenced by a name. parameter A variable has: a value and zero or more attributes (such as integer, ...). Attributes are assigned using the declare builtin command....
Bash Liste Des Attaques Ovh
Bash - Case

The case control statement execute commands based on regular expression pattern matching. Initscript example: Selectively execute COMMANDS based upon WORD matching glob PATTERN. The | is used to...
Bash Liste Des Attaques Ovh
Bash - Conditional Expression

A predicate or boolean expression is known in Bash as a Conditional Expression. It is a command that returns an exit status of 0 or 1. The conditional expression is part of the compound expression. ...
Bash Liste Des Attaques Ovh
Bash - Expansion

This article is expansion in Bash. An expansion is the replacement of a special token in your code by the result of the expansion during code execution. It's performed on the command line after it has...
Bash Liste Des Attaques Ovh
Bash - Parameter Expansion ${

Parameter expansion are patterns applied to the parameters that have different results. Same as ?? See also: The value of parameter is substituted where: the braces are optional. They served...
Bash Liste Des Attaques Ovh
Bash - (Current|Working) Directory

This article is the management of the current directory in Bash. You can query the current working directory via: the pwd utility or via the PWD environment variable shell variable concerning...



Share this page:
Follow us:
Task Runner