I'm new to linux and starting from the basics.
-- I thought alias is used to make a shortcut to a command. But I tried the following using a variable (in Ubuntu) and still works!
$ foo="mkdir Directory"
$ $foo #this will create a directory named Directory
using alias:
$ alias bar="mkdir Directory"
$ bar #creates a Directory named directory
Is that how it is supposed to work? Many thanks for the answers :)
An alias occurs when different variables point directly or indirectly to a single area of storage.
Bash aliases allow you to set a memorable shortcut command for a longer command. Bash aliases are essentially shortcuts that can save you from having to remember long commands and eliminate a great deal of typing when you are working on the command line.
No, an assignment of a value to a variable is not the same as creating an alias. Creating an alias is not an assignment from the point of view of the shell.
Functions can be used in scripts or in the console, but are more often used in scripts. Contrary to aliases, which are just replaced by their value, a function will be interpreted by the bash shell. Functions are much more powerful than aliases. They can be used to build very complex programs.
Variables are much more versatile than aliases. Variables can be used anywhere in a command line (e.g. as parts of program arguments), whereas aliases can only be used as the names of programs to run, i.e. as the first word in a 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 into the environment of child processes. If you use the export
builtin to export variables, then programs can use getenv(3)
function to get the variables' values.
See the Bash manual for a full description of all of the different types of expansions it can perform and how it performs them. See also the section on aliases.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With