export command is used to export a variable or function to the environment of all the child processes running in the current shell. export -f functionname # exports a function in the current shell. It exports a variable or function with a value. “env” command lists all the environment variables.
Persisting Environment Variables for a UserWhen an environment variable is set from the shell using the export command, its existence ends when the user's sessions ends. To make an environment persistent for a user's environment, we export the variable from the user's profile script.
For example, Create the variable called vech, and give it a value "Bus":
- vech=Bus. Display the value of a variable with echo, enter:
- echo "$vech" Now, start a new shell instance, enter:
- bash.
- echo $vech.
- export backup="/nas10/mysql" echo "Backup dir $backup" bash echo "Backup dir $backup"
- export -p.
The set command assigns a value to a variable (or multiple values to multiple variables). A single set command can be used to set many variables, but such a use is not recommended. Note. set is for setting shell variables, which do not propagate to child shells.
An environment variable is a dynamic "object" on a computer, containing an editable value, which may be used by one or more software programs in Windows. Environment variables help programs know what directory to install files in, where to store temporary files, and where to find user profile settings.
The first way of setting your $PATH permanently is to modify the $PATH variable in your Bash profile file, located at /home/<user>/. bash_profile . A good way to edit the file is to use nano , vi , vim or emacs . You can use the command sudo <editor> ~/.
To Set PATH on Linux
- Change to your home directory. cd $HOME.
- Open the . bashrc file.
- Add the following line to the file. Replace the JDK directory with the name of your java installation directory. export PATH=/usr/java/<JDK Directory>/bin:$PATH.
- Save the file and exit. Use the source command to force Linux to reload the .
Steps to write and execute a script
- Open the terminal. Go to the directory where you want to create your script.
- Create a file with . sh extension.
- Write the script in the file using an editor.
- Make the script executable with command chmod +x <fileName>.
- Run the script using ./<fileName>.
source is a bash built-in command so to execute source command, you can log in as Root. Bourne shell (sh) uses PATH to locate in source <file> .
In Linux, ./ refers to the current directory. Let's take an example. You open up your command prompt because you are huge console junky and you would rather use emacs, a text editor for the console, than a graphic word processor. So you open up the terminal, where you are, by default, placed in the home directory.
source is a shell built-in command which is used to read and execute the content of a file(generally set of commands), passed as an argument in the current shell script. If any arguments are supplied, they become the positional parameters when filename is executed.
To invoke a bash function, simply use the function name. Commands between the curly braces are executed whenever the function is called in the shell script. The function definition must be placed before any calls to the function.
Answer. Source evaluates any file written after, the file if containing any commands to run, it will execute them in the order they were written in the file. for example if our file had: echo 'This is the first one' echo 'second' echo 'and third' when running: source <ourFilename> or . <
When a file is sourced (by typing either source filename or . filename at the command line), the lines of code in the file are executed as if they were printed at the command line. This is particularly useful with complex prompts, to allow them to be stored in files and called up by sourcing the file they are in.
- The source command can be used to load any functions file into the current shell script or a command prompt.
- It read and execute commands from given FILENAME and return.
- The pathnames in $PATH are used to find the directory containing FILENAME.
The procedure to run the .sh file shell script on Linux is as follows:
- Set execute permission on your script: chmod +x script-name-here.sh.
- To run your script, enter: ./script-name-here.sh. Another option is as follows to execute shell script: sh script-name-here.sh. OR bash script-name-here.sh.
The recommended way to evaluate arithmetic expressions with integers in Bash is to use the Arithmetic Expansion capability of the shell. The builtin shell expansion allows you to use the parentheses (()) to do math calculations. The format for the Bash arithmetic expansion is $(( arithmetic expression )) .
'declare' is a bash built-in command that allows you to update attributes applied to variables within the scope of your shell. In addition, it can be used to declare a variable in longhand. Lastly, it allows you to peek into variables.
To create a variable, you just provide a name and value for it. Your variable names should be descriptive and remind you of the value they hold. A variable name cannot start with a number, nor can it contain spaces. It can, however, start with an underscore.
Create an array
- Create indexed or associative arrays by using declare. We can explicitly create an array by using the declare command: $ declare -a my_array.
- Create indexed arrays on the fly.
- Print the values of an array.
- Print the keys of an array.
- Getting the size of an array.
- Deleting an element from the array.
In general, the export command marks an environment variable to be exported with any newly forked child processes and thus it allows a child process to inherit all marked variables.
The expr command in Unix evaluates a given expression and displays its corresponding output. It is used for: Basic operations like addition, subtraction, multiplication, division, and modulus on integers. Evaluating regular expressions, string operations like substring, length of strings etc.