Linux source command directory

The “source” Command in Linux [3 Practical Examples]

The source command in Linux executes commands from the file passed as an argument to it in the current shell environment. This command is beneficial for sourcing a script that sets variables, functions, or aliases in the current shell session.

A. Description

The source command serves the purpose of running the command from a file sequentially.

B. Syntax

The source command in Linux is a command that takes the filename and argument. The syntax for source commands is as follows.

Note: Here, the argument is enclosed by square brackets which refers that argument is not mandatory for the command.

C. Options

The source command in Linux is a shell built-in command. That’s why It doesn’t contain any man page. Moreover, there is not any option available for this command. To know more about this command, you can check its help page by executing the below command.

Practical Examples of the “source” Command in Linux

The source command in Linux is a handy tool to execute commands from a file. The source command in Linux has many practical applications, and a few of them are outlined below. Download this file to work with the “source” command in Linux

Example 1: Executing Multiple Commands Line by Line from a File in the Current Directory.

command1.txt file contains ls and date command.

Using the source command, you can execute multiple commands line by line from a file. I have a file named command1.txt in my current directory, which contains the ls and date commands sequentially. I will use the source command to execute these commands from the command1.txt file. To do this task, follow the steps given below. Steps to Follow: ➊ At first, open the Ubuntu Terminal. ➋ Then, copy the following command in the command prompt:

The source command in Linux executes the ls and date commands sequentially from the file command1.txt in the current directory.

➌ Now, press the ENTER button. Output: The source command executes the ls and date commands sequentially from the file command1.txt in the current directory, as illustrated in the following image.

  • The “alias” Command in Linux [3 Practical Examples]
  • The “unalias” Command in Linux [3 Practical Examples]
  • The “exit” Command in Linux [5 Practical Examples]
Читайте также:  Linux apt get aptitude

Example 2: Executing Multiple Commands Line by Line from a File of a Different Directory

Using the “source” Command in Linux.

command2.txt contains some commands.

You can execute commands from a file in a different directory by specifying the path to that file. I have a file named command2.txt at ~/Desktop/ directory. In this example, I will be working with the command2.txt file which contains the following contents. Now, I will execute commands from this file using the source command. To do so, follow the below procedures.

Steps to Follow:

➊ At first, open the Ubuntu Terminal.

➋ Then, insert the following command in the command prompt.

source ~/Desktop/command2.txt

➌ Now, Hit the ENTER button.

The source command executes the commands from the file command2.txt from a different directory.

The following image depicts that I have executed some desired commands from a file named command2.txt from a different directory with the source command.

Example 3: Executing Command Line by Line from a File in Bash Script Using the “source” Command in Linux.

You can execute commands of a file inside a bash script. Here, I have a file named config.sh where the value of V1 and V2 are written. I will read the value of V1 and V2 from the sample_script.sh file using the source command. To achieve so, follow the below procedures.

Steps to Follow:

➊ At first, launch the Ubuntu Terminal.

➋ Then, type the commands in the command prompt as given below.

➌ Now, tap the ENTER button.

The source command in Linux has read the value of V1 and V2 from the bash script named sample_script.sh. The value of V1 and V2 was originally stored in the config.sh file.

The source command has read the value of V1 and V2 from the bash script named sample_script.sh, as shown in the following image. The value of V1 and V2 was originally stored in the config.sh file.

Conclusion

In this article, I have demonstrated some examples of the source command in Linux, which is a handy tool for executing multiple commands of a file sequentially. I hope you’ll be competent enough to explore more things with the help of these practical examples.

Related Articles

  • The “bash” Command in Linux [5 Practical Examples]
  • The “clear” Command in Linux [3 Practical Examples]
  • The “declare” Command in Linux [7 Practical Examples]
  • The “export” Command in Linux [8 Practical Examples]
  • The “echo” Command in Linux [7 Practical Examples]
  • The “hash” Command in Linux [6 Practical Examples]

Источник

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.

Читайте также:  Linux по русски awk

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 −

Читайте также:  Cannot find device linux

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.

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.

Источник

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