Which Characters Are Allowed in a Bash Alias

Can the '-' character, as is, be an alias in bash?

The behavior you want depends on shell-specific extensions; even when the POSIX standard does specify alias behavior (which is only the case for shells implementing both XSI and user portability extensions), the set of allowed names is not required to include either - or =:

3.10 Alias Name

In the shell command language, a word consisting solely of underscores, digits, and alphabetics from the portable character set and any of the following characters: '!', '%', ',', '@'.

Implementations may allow other characters within alias names as an extension.


That said, when defining an alias in bash, -- can be used to cause subsequent arguments not to be parsed as options (per POSIX syntax guidelines entry #10):

alias -- -=date

Another option available in practice with bash (tested on both 3.2.57(1) and 4.3.46(1), but not required by the POSIX standard to be supported with these names) is to define functions:

$ =() { date "$@"; }
$ -() { date "$@"; }
$ =
Sat Aug 13 18:12:37 CDT 2016
$ -
Sat Aug 13 18:12:08 CDT 2016

Again, this goes beyond the set of names required by POSIX:

2.9.5 Function Definition Command

The format of a function definition command is as follows:

fname() compound-command[io-redirect ...]

The function is named fname; the application shall ensure that it is a name (see the Base Definitions volume of IEEE Std 1003.1-2001, Section 3.230, Name). An implementation may allow other characters in a function name as an extension. The implementation shall maintain separate name spaces for functions and variables.

3.230 Name

In the shell command language, a word consisting solely of underscores, digits, and alphabetics from the portable character set. The first character of a name is not a digit.

...and, thus, being defined neither by POSIX nor by bash's own documentation, may be subject to change in future releases.

how to use control characters in bash alias definition

$n is expanded in " quotes. Because most probably variable n is empty, it expands to nothing. Escape it.

alias mypr="enscript -jC -p output.ps -b '\$n %W Page \$% of \$='"

Or use single quotes:

alias mypr='enscript -jC -p output.ps -b '\''$n %W Page $% of $='\'

Or just use a function:

mypr() { enscript -jC -p output.ps -b '$n %W Page $% of $=' "$@"; }

Escaping characters in bash alias

Best Advice: Don't.

Use a function instead:

th() { tmux new -s "${PWD##*/}" "$@"; }

${PWD##*/} is a parameter expansion which strips everything up to and including the last / from the contents of $PWD.


Alternate Approach: Literal Quotes

The issue in your original code is that it contains syntactic quotes -- ones parsed by the shell to determine where single-quoted parsing rules begin and end -- in places where what you actually want is literal quotes, ones which are treated as data (and thus become part of the alias).

One way to make these quotes literal would be to use the $'' quoting form instead, which lets you use literal backslashes to escape inner quotes, making them literal rather than syntactic:

alias th=$'tmux new -s $(pwd | tr \'\\\/\' \'\\n\' | tail -n 1)'

Note that when using $'', literal backslashes need to be escaped as well (thus, written as \\ rather than \).


Explanation: Why

The quoting of strings in POSIX shell languages is determined on a character-by-character basis. Thus, in the case of:

'$foo'"$((1+1))"baz

...$foo is single-quoted and thus treated as a literal string, $((1+1)) is double-quoted and thus eligible for being treated as arithmetic expansion, and baz is unquoted -- even though all three of these are concatenated to form a single word ($foo2baz).

These quotes are all syntactic -- they're instructions to the shell -- not literal (which would mean they'd be part of the data to which that string evaluates).


How This Applies To Your Previous Command

In

alias th='tmux new -s $(pwd | tr '\/' '\n' | tail -n 1)'  

...the single quotes in the arguments to tr end the single quotes started at the beginning of the alias. Thus, \/ and \n are evaluated in an unquoted context (in which \/ becomes just /, and \n becomes just n) -- and since, as described above, multiple differently-quoted substrings can just be concatenated into a single larger string, you get your prior command, not an alias.

What characters can I use in a Git alias?

From the git-config man page

The variable names are case-insensitive, allow only alphanumeric
characters and -, and must start with an alphabetic character

Git aliases are variables in the git config options (e.g. in the .gitconfig file).

So, git aliases are case-insensitive, and neither "my.branch" nor "@mine" can be aliases.

Make a Bash alias that takes a parameter?

Bash alias does not directly accept parameters. You will have to create a function.

alias does not accept parameters but a function can be called just like an alias. For example:

myfunction() {
#do things with parameters like $1 such as
mv "$1" "$1.bak"
cp "$2" "$1"
}

myfunction old.conf new.conf #calls `myfunction`

By the way, Bash functions defined in your .bashrc and other files are available as commands within your shell. So for instance you can call the earlier function like this

$ myfunction original.conf my.conf


Related Topics



Leave a reply



Submit