What is a Bash Source Command
Here you will find out:
- what a bash source command is and its syntax
- how to use the source command
- when DiskInternals can help you
Are you ready? Let's read!
What is a bash source command used for?
If you run Linux then you definitely know this command. Otherwise - you need to meet it! The bash source command reads and launches instructions that are given from the file that is specified as an argument in the shell environment. You can use it to load in shell scripts: variables, functions and configuration documents.
In simple terms, this built-in feature is called to load any feature file(as a parameter) into the current shell script or command prompt.
The syntax for the source command
The syntax is as follows:
In the example above, you can see the dot (.) - it is called a period and this is equal to the source bash script.
It will only read and execute commands from a given filename in the current shell enviroment. The entries in $path are used to look up for the directory containing filename.
In case if filename is not the full path of the file, the command searches for the selected file in the directory specified by the $path.
Again, if the file is still not found in $path - the current directory will be checked.
What if some arguments exist? It will be encountered as positional parameters to the filename.
The exit status of the source command executed: 0 - if the asked and if the file filename exist, and 1 - if the filename not found (doesn’t exist).
Examples of using the source command
Read variables from a file
As you can guess, using bash source command you can read variables from a file. Yes, this command can be useful for different purpose and to read variables from a file, run this bash syntax: variable=value.
Using source command to read the configuration file, you will get:
Output:
Import a script function
To import a script function - it is the main purpose of the source command usage. It is helpful to update the script function - so you can use existing shell functions more efficiently.
Let’s see how it works:
First, you need to create a new script file. Input the following content:
Now you need to create another script to import your printUser function. How can you do that? Well, that is why the source command exists:
Now, it is time to assign the correct permissions to the file to be launched. No need in execute permissions on the file that contain the features to be imported:
As the output you will see the name of the current user.
About Exit Status
Every Linux or Unix command executed by a shell script or user has an exit status. The exit status can be called an integer.
This command returns the status of the last command executed in filename; if it cannot read filename it fails.
An exit status of 0 indicates that the command was successfully executed without any errors. A non-zero (1-255 value) exit status indicates that the command failed.
Open Linux files from Windows
DiskInternals Linux Reader is a popular free software product and it is still non-commercial free software. Starting with version 4.0, Linux Reader Pro provides additional features: access to DVR, Hikvision NAS, ZFS and XFS.
If you are running Linux on a dual-boot or virtual machine, you can use DiskInternals Linux Reader to easily transfer files, read and mount system files. This easy to use, super simple and professional program can help you meet all your data needs.
DiskInternals Linux Reader there is to give you secure, read-only access to the source drive without any hindrance.