Aliases and Functions :
- The entire contents of aliases and functions are stored in the shell’s memory.
- A natural consequence of this is aliases and functions can only be used by the current shell, and not by any
- other programs you may invoke from the shell like text editors, scripts, or even child instances of the same shell.
- Aliases and functions are executed by the current shell, i.e. they run within and affect the shell’s current
- environment. No separate process is necessary to run an alias or function.
- Shells do not keep scripts in memory. Instead, scripts are read from the files where they are stored every time they are needed. If the script is found via a $PATH search, many shells store a hash of its path name in memory to save time on future $PATH look-ups, but that is the extent of a script’s memory footprint when not in use.
- Scripts can be invoked in more ways than functions and aliases can. They can be passed as an argument to an interpreter, like sh script, or invoked directly as an executable, in which case the interpreter in the shebang line (e.g. #!/bin/sh) is invoked to run it. In both cases, the script is run by a separate interpreter process with its own environment separate from that of your shell, whose environment the script cannot affect in any way.
- Indeed, the interpreter shell does not even have to match the invoking shell. Because scripts invoked this way appear to behave like any ordinary executable, they can be used by any program.
- Finally, a script can be read and run by the current shell with ., or in some shells, source. In this case, the script behaves much like a function that is read on-demand instead of being constantly kept in memory.
When to write an alias:
Bash aliases is not working inside shell script constructs like if/then and for/do loops, so should only be used as keyboard shortcuts on an interactive commandline. Thus, all alias definitions usually reside inside the appropriate login script like ~/.profile or ~/.bashrc.
When to write a script:
- Scripts assemble software components (aka. tools, commands, processes, executables, programs) into more complex components, which may themselves be assembled into still more complex components.
- Scripts are usually made executable so they can be called by name. When called, a new subprocess is spawned for the script to run in. Copies of any export ed variables and/or functions are passed by value to the script. Changes to those variables do not propagate back to the parent script.
- Scripts may also be loaded(sourced) as if they were part of the calling script. This is analogous to what some other languages call “import” or “include”. When sourced, they execute within the existing process. No subprocess is spawned.
When to write a function:
- Functions are effectively pre-loaded shell scripts. They perform a bit better than calling a separate script, but only if it must be read from mechanical disk. Today’s proliferation of flashdrives, SSDs and Linux’s normal caching in unused RAM make that improvement large.
- Functions serve as bash’s principle means of achieving modularity, encapsulation and reuse. They improve the clarity, reliability and maintainability of scripts.
- The syntax rules for calling a function are identical to that of calling an executable. A function with the same name as an executable would be invoked instead of the executable.
- Functions are local to the script . They are invisible to the process that calls that script (e.g. commandline, another script or a program).
- Functions may be exported (copied by value) so they can be used inside called scripts. Thus, functions only propagate to child processes, never parents.
- Functions create reusable commands that are often assembled into libraries (a script with only function definitions) to be sourced by other scripts.
Example for alias , you want use straight to desktop in commandline, so you can do this
From then on you just type desktop in terminal and it will perform the cd ~/Desktop for you.
Example for functions: It contains logic. In a function, you might make calls to several different programs. Here’s a simple echo function
While it may appear similar to an alias when you call it e Hello then e() can have a lot of different things happen.
Logs all results to file myfile.txt. This will open a subshell and records all information through this session. The script ends when the forked shell exits (e.g., when the user types exit) or when CTRL-D is typed.
The output will be written to the file ./myfile.txt, and the last line of the file will log the date and time the command was executed.
Logs the output of the echo command, which simply echoes the text “Hello, World!”. Since the entire command must appear in quotes, and the command contains quotes itself, the quotes in the command are escaped with a backslash. This “protects” them from the shell, telling it to treat them as literal quotes. Alternatively, we could have used single-quotes to enclose the command, and double-quotes inside the command to differentiate them.