Bash Command: What is the difference between a variable and an alias?

I am new to Linux and start with the basics.

- I thought the alias was used to create a shortcut for the team. But I tried using a variable (in Ubuntu) and it still works!

$ foo="mkdir Directory" $ $foo #this will create a directory named Directory 

using an alias:

 $ alias bar="mkdir Directory" $ bar #creates a Directory named directory 

Is that how it should work? Thanks so much for the answers :)

+8
linux bash terminal ubuntu
source share
5 answers

Variables are much more universal than aliases. Variables can be used anywhere on the command line (for example, as parts of program arguments), while aliases can only be used as the names of programs to run, that is, as the first word on the command line. For example:

 foo="mkdir Directory" echo $foo # Prints "mkdir Directory" alias bar="mkdir Directory" echo bar # Nothing gets expanded -- just "bar" is printed 

Variables can also be exported to the environment of child processes. If you use export builtin to export variables, then programs can use getenv(3) to get the values ​​of the variables.

See the Bash manual for a complete description of all the different types of extensions that it can run and how to execute them. See also aliases section.

+11
source share

Note that when entering an alias, as if it were a command, you do not enter $ ; when you enter a variable, you need to enable it. Thus, aliases act more naturally as an alternative name for the command.

+1
source share

In variable form, it is expanded and evaluated.

Consequently

 $ $foo 

expands to

 $ mkdir Directory 

which can be appreciated. This is the same type of extension as foo is the argument:

 $ echo $USER 

For aliases, you refer to them directly using the name (no $), and it expands only if it is the first word.

+1
source share

"Variables are much more universal than aliases" - not entirely true!

To execute commands, variables have some restrictions that are not shared by aliases.

Try using a variable to define a command that echoes a single asterisk on the command line:

 $ myCommand="echo *" $ $myCommand file1.x file2.y file3.z 

Extending * is not what you wanted, so try the following:

 $ myCommand="echo \"*\"" $ $myCommand "*" 

But now you have extra quotation marks β€” you didn't want that either!

But this works fine:

 $ echo "*" * 

So:

 $ alias myCommand="echo \"*\"" $ myCommand * 

This is because the quotes inside the variable are treated as alphabetic rather than syntactic when the variable is expanded - therefore, in this case they become part of the parameter that is passed to the echo command, while the β€œas is” command is executed with the alias and the quotes are considered as syntactic and are analyzed before the echo is called, just like when you enter the same command directly from the command line.

+1
source share

Variables replace their contents wherever they are used.

 $ foo="mkdir Directory" $ $foo $ echo "$foo" 

Aliases act as valid commands, or rather, the prefix of one.

 $ alias bar="mkdir Directory" $ bar directory2 #creates directories named "directory" and "directory2" $ echo bar 
0
source share

All Articles