Linux source shell script

How to execute script in the current shell on Linux?

Is there a way to mark a script to be executed in the current shell from whitin It? I know I can use:

but I need a way to include the «dot» way inside the script. Edit: Here is the case. I have a script «myscript.sh» located in directory A:

rm -r A mkdir A touch A/test.file cd A 

There is no problem. I need a way to include this «dot» way inside my script so It can be called without It.

The «dot» is shorthand for the «source» command. If I understand you correctly, this cannot be done. The script itself cannot decide if it is to be called or sourced, the calling shell has to decide that.

5 Answers 5

There are two ways of executing a script in the shell. Depending upon your needs, you will have to do one of the following (don’t do both!).

  1. Launch the script as a program, where it has its own process.
  2. Source the script as a bunch of text, where the text is processed by the current shell.

To launch the script as a program:

This will be read by the loader, which has special logic that interperts the first two characters #! as «launch the program coming next, and pass the contents into it». Note this only properly works for programs written to receive the contents

To source the script into the current shell:

Note: . in bash is equivalent to source in bash.

This acts as if you typed in the contents of «script.sh». For example, if you set a variable in «script.sh» then that variable will be set in the current shell. You will need to undefine the variable to clear it from the current shell.

This differs heavily from the #!/bin/bash example, because setting a variable in the new bash subprocess won’t impact the shell you launched the subprocess from.

Источник

Linux source Command

The Linux source command is an essential tool for Linux users and administrators. It is used to execute a script file in current shell environment, and it allows you to modify current shell environment in same way that you would if you had typed commands manually. In this article, we will explore Linux source command and its various applications.

What is Linux source command?

The Linux source command is a shell command that reads and executes commands from a file in current shell environment. file is typically a shell script, but it can also be any text file containing a series of commands. source command is often used to set environment variables, define functions, and execute initialization scripts.

Читайте также:  Install linux with vmware player

Syntax of Linux source command

The syntax of Linux source command is straightforward −

In this syntax, filename parameter represents name of file containing commands that you want to execute.

Using Linux source command

To use Linux source command, you must first create a script file containing commands that you want to execute. Once you have created script file, you can use source command to execute commands in file.

#!/bin/bash export MY_VAR="Hello World"

In this example, we’ve created a simple script file that defines an environment variable called MY_VAR and sets its value to «Hello World». To execute this script using source command, we would run following command −

This command would read contents of myscript.sh file and execute commands in current shell environment. After running this command, MY_VAR environment variable would be set to «Hello World».

The source command can also be used to execute initialization scripts. For example, many Linux systems include an initialization script that is executed when system boots up. You can use source command to execute this script manually if you need to −

This command would execute rc.local script located in /etc/init.d directory.

Using Linux source command with functions

The Linux source command is particularly useful when working with shell functions. Shell functions are a way to group a set of commands together and give them a name. You can then call function by its name to execute commands.

Here’s an example of a simple shell function −

In this example, we’ve defined a function called myfunction that simply echoes message «Hello World». To execute this function using source command, we would run following command −

source myscript.sh myfunction

This command would read contents of myscript.sh file and define myfunction function in current shell environment. We could then call function using its name to execute commands.

Using Linux source command with variables

The Linux source command can also be used to set and modify variables in current shell environment. For example, let’s say we have a script file called myvars.sh that defines two variables −

#!/bin/bash MY_VAR1="Hello" MY_VAR2="World"

To set these variables in current shell environment using source command, we would run following command −

After running this command, MY_VAR1 and MY_VAR2 variables would be defined in current shell environment, and we could access their values using $MY_VAR1 and $MY_VAR2 variables.

Using Linux source command to modify PATH variable

The Linux source command can be particularly useful when modifying PATH environment variable. PATH variable is used by shell to locate executable files, and it typically includes a list of directories separated by colons.

To modify PATH variable, you can create a script file that defines new PATH value and then use source command to execute script in current shell environment. Here’s an example of a script file that adds a directory to PATH −

#!/bin/bash export PATH="/my/new/directory:$PATH"

In this example, we’ve added /my/new/directory directory to beginning of PATH variable. To execute this script using source command, we would run following command −

After running this command, PATH variable would be updated to include new directory at beginning of list.

Additional Subheadings and Examples

Using Linux source command to source bash aliases

The source command can be used to source Bash aliases. An alias is a way to create a shorter name for a longer command, making it easier to type and remember. To use source command to source Bash aliases, you would create a Bash file with alias commands and then run source command on that file. Here’s an example −

# in your ~/.bash_aliases file: alias ll='ls -alF' # source ~/.bash_aliases file source ~/.bash_aliases

Now, when you run ll command in your terminal, it will be translated to ls -alF.

Читайте также:  Checking port used in linux

Using Linux source command in shell scripts

The source command can also be used in shell scripts to load environment variables and functions from other files. This can be useful when you have common functionality that you want to use across multiple scripts. Here’s an example −

# in your common_functions.sh file: my_function() < echo "Hello World!" ># in your script.sh file: source common_functions.sh my_function

In this example, source command loads my_function function from common_functions.sh, which is then called in script.sh.

Using Linux source command in virtual environments

The source command can be used in virtual environments to activate them. Virtual environments are isolated Python environments that can have their own installed packages and versions. source command can be used to activate virtual environment and load its dependencies. Here’s an example −

# create and activate a virtual environment python -m venv myenv source myenv/bin/activate

In this example, source command activates virtual environment created in myenv directory.

Using Linux source command with Git

The source command can be used with Git to load aliases and functions that can make working with Git easier. Git aliases are shortcuts for longer Git commands. Here’s an example of how to use source command with Git −

# in your ~/.gitconfig file: [alias] st = status co = checkout br = branch # source ~/.gitconfig file source ~/.gitconfig

In this example, source command loads Git aliases defined in ~/.gitconfig.

Conclusion

The Linux source command is a powerful tool that can be used to execute script files in current shell environment. It is particularly useful when working with shell functions and modifying environment variables such as PATH. By using source command, you can modify shell environment in same way that you would if you had typed commands manually. This can save you time and make your work more efficient.

Источник

How to Use Bash Source Command

The Bash source is an in-built command used to read and execute commands from a file and, in some cases, pass them as arguments in the current shell. You can load functions and variables into the current shell scripts with the source command. Moreover, you can use the source command on the terminal or in a bash script, especially to load functions and variables from other functions.

When used in the terminal to execute a given script, the script gets executed in the same shell from where it gets sourced. Therefore, the script can access all variables in the shell where they have been sourced. In this case, the source replaces the period (.) used when executing a bash script.

However, if you just execute a script by typing its name or using the bash command, a new instance gets created, and the script can only access exported variables or those in its parent shell.

Читайте также:  Uefi usb boot linux

How to use the source command

You can use the source command when working with bash scripts in various ways.

1. Executing Functions on Terminal

If you have a bash script, you can use source to execute it instead of using a period. The syntax for that is shown below.

Look at the example below, where we’ve executed a bash script using source and period.

The two outputs are the same in executing the script. However, with source, you can use the current and the parent variables, even those defined, without using the export keyword.

2. Importing Functions on Another Script

You can also execute functions in another bash script to build a library of functions. Let’s have a script named demo1.sh with one function that prints the uptime.

Let’s create another script, verify.sh. If we needed to access the function from another bash script, we could achieve that using the source command. You first source the name of the file.

Once that is done, you can proceed to use functions from the other script on the current script.

You can execute the script on the terminal to confirm that it works.

3. Importing Functions on Terminal

You can also import a function into the current shell. To do that, you first source the file.

Once you have the file imported, use its functions on the terminal.

4. Updating Variable Values

Suppose you needed to update the values of variables in a given script regardless of its location. In that case, you could source the script, then update the variable without using the export command. In this example, we are updating a script named linuxhint.sh

Let’s start by sourcing the file, and we can do that by adding its full path.

Once we’ve sourced it, go ahead and update the variable.

Execute the script using the source command to verify the updates.

5. Passing Environmental Variables

When using Source, you can also import environmental variables when writing a script. You could choose to read and set various environmental variables, or collect any variable from the imported file, and use it in your script.

In such a case, all you need to do is to source the path to the environmental variable; from there, you can use any of its functions.

For instance, let’s create a script that sources the ~/. bashrc and gets a value from the imported environmental variable.

Executing the script gives a value from the imported environmental variable file, as shown in the image below.

Those are the common uses of the Bash source command.

Conclusion

The source command is a helpful bash command that lets you easily work with scripts. We’ve seen how you can use it to execute Bash scripts, import functions, update variables, and pass environmental variables from a file. Try using the source file following the examples provided in the article, and if you get stuck, refer to the man page for more clarity.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.

Источник

Оцените статью
Adblock
detector