Linux source file example

Linux Source Command with Examples

Execute the “test.sh” file using the bash command, and you can see it shows the output statements and the variables declared in the main.sh file. $ bash test.sh Example 05: It will be our last example for source command. $ source / home / aqsayasin / test.txt Another method to execute the files is by just using the name of a file in the command.

Linux Source Command with Examples

The source seems to be an integrated shell instruction that reads and executes the contents of a document (commonly a bundle of instructions) that is provided as a parameter within the present shell script. Upon obtaining the contents of the given files, the instruction delivers them to the TCL interpreter only as a textual program , which would then be run. Whenever filename is called, any arguments given turn out to be positional arguments. The positioning variables, on the other hand, stay unaltered. The elements in $PATH have been used to locate the folder that contains FILENAME; nevertheless, unless the document isn’t found in $PATH, it would be searched in the root folder. The source command seems to have no options, and indeed the document seems to be the only parameter. Let’s get started working on the Linux Source Command in Ubuntu 20.04 Linux operating system. Login from your system and launch the Ubuntu terminal by utilizing the shortcut Ctrl+Alt+T.

Example 01:

Create a text file named “test.txt” by consuming the “touch” instruction beneath and press Enter to execute it.

Open the home directory of your Linux system via File Explorer, and double tap on the newly created file “test.txt.” We wrote the below-displayed code in it, having two echo statements to display some data in the terminal. After that, we have used the word “pwd,” which indicates the method to show the current directory of the file. Hence, “pwd” will show the current directory of a file. Another echo statement has been used to show some data. The date built-in method has been called to show the current date. Save your file by pressing Ctrl+S.

There are two ways to execute a file with the source command. One method is via providing the whole path of a file within the source command as below. It will execute the file and show the current directory of a file where it resides the current date and time.

Another method to execute the files is by just using the name of a file in the command. It will search this file in the root directory of a system. The output would be the same as above.

Example 02:

Let’s have another example to see the working of a source command in the shell. First, create a new text file “new.txt” by utilizing the “touch” keyword within a command as below.

Читайте также:  Копирование файлов терминале linux

Navigate towards the root directory of your system via the file explorer held on your desktop. Please open the file new.txt by double-tapping on it. Now we have added the below-shown script. The first line contains the keyword “ls” to show the list of all files and folders in the directory we are currently working in. Another two keywords have been used to indicate the date and time of the current timezone you are working in. Save the code file and close it.

Open the terminal once again, execute the source command and the name of a file “test.txt” as below. Don’t forget that we have been using the source command to locate the file in our system’s home or root directory. When this file gets executed, you can see it shows the list of files and folders residing in the home directory and the current date and time. One thing to be noted is that the file we have been running using the source command has been highlighted in the output. Another point is that if the file “new.txt” is not located in the home directory, the query may not work fine.

Example 03:

In the above examples, we have used single files to elaborate the working of a source instruction. This time we will be using two files to see the functioning of the Source command in Linux . On the other hand, we will be using the bash file s instead of simple text files this time. You need to understand that we can use the script functions in another file while sourcing. Hence, open the shell and make a first bash file named “main.sh” using touch as follows:

Let’s open the file “main.sh” from the root directory after double-tap on it. Write out the below-presented code in your file. We have added the bash support in our file first. After that, we have created a function “func” to add some instructions to it. We have added the if statement to check if the USER is root or not. If the user is not root, it will display “This code must be executed as a root.” The loop ends and the function as well. Save and close your file.

When you run this bash file, it will display nothing because the function “func” has not been called anywhere in the code.

Let’s create another bash file, “test.sh”.

Open the test.sh file from the home directory, and add the below script. This file has been sourcing the main.sh file in it. After sourcing the file main.**** calls the function “func” implemented in the file main.sh. Another echo statement has been used to display “I am sudo/root user” outside the function.

Execution of the test.sh file using bash command shows us the statement from the function “func” of the main.sh file because we have been logged in from a non-root user.

Читайте также:  How to create directories in linux terminal

Let’s log in as a root user using the “su” command and then add the sudo password. Again enter the bash command to execute the file. This time, the echo statement from the “test.sh” file has been executed in the output as the if statement from main.sh file doesn’t meet its requirements.

Example 04:

Let’s have another illustration. Open the file main.sh, and write the below script having the support of bash. We have added two variables, “one” and “two,” with some values in both. Save the file once again.

Now, open the test.sh, file, and update its code as below. We have been sourcing the “main.sh” file in it. After that, we have been printing the statements along with the variables declared in the main.sh file.

Execute the “test.sh” file using the bash command, and you can see it shows the output statements and the variables declared in the main.sh file.

Example 05:

It will be our last example for source command. Update the main.sh file as below. This time, we have used a new method, “show(),” to display the USER currently logged in.

We have updated the file test.sh by sourcing main.sh file in it and calling the function “show().”

Execution of the file test.sh shows the current user “aqsayasin” as a result because we are currently working on it.

Conclusion:

We learned well how to utilize the source constructed command within our command line in this article. Both bash and source command looks the same while working, But apparently, they are a bit different. Both instructions may, even so, have been used to run a script.

How to define a shell script to be sourced not run, Under bash, $ will contain the name of the current file that the shell is reading regardless of whether it is being sourced or

Shell Script : source file not found error

Hello Guys, in this video I going to cover Shell Script : source file not found error | Shell Script Duration: 2:27

Source Shell Script vs Executing Shell Script

Source Shell Script vs Executing Shell Script — The Real DifferenceWhat is the Real Difference Duration: 5:47

Linux source command summary with examples

This Linux source command tutorial shows you how to read and execute commands from a file
Duration: 3:37

How to use source command in shell script?

I would like to use source command in shell script.

What I have done is like this below:

source ~/tensorflow/bin/activate 

then in command line run this script.

However nothing happens and

command doesn’t show anything.

So, source is not the normal command?

Is this wrong idea? or how can I reduce the type of path?

source is a shell built-in command. The which command looks for binaries on the PATH , like in /usr/bin, /bin, /sbin, etc. but you won’t find any built-in commands in a separate binary.

Also, having the source command in a shell script does not result in the source propagating up to your current shell when you run it. sh blah.sh where blah.sh has source in it will not actually source the contents of the file into your interactive shell. That’s not how sourcing works.

Читайте также:  Linux on blackberry passport

If you want this sourcing of the tensorflow activate script to happen every time you open a new shell, you need to edit ~/.bashrc or ~/.profile (or other files, depending on what your shell is and how it’s configured) and put the source` command directly in there.

P.S. — your question title is very confusing and looks incomplete. Take some time to edit, revise and clean up your post, or you run the risk of someone downvoting it 😛 I’m tempted to do so myself, but I wrote an answer, so I’m a little bias.

I came across this very problem, when I wanted to avoid writing:

source ~/.bashrc source activate tensorflow jupyter notebook 

Thereafter, I came across a page which explains this better: What are the differences between executing shell scripts using “source file.sh”, “./file.sh”, “sh file.sh”, “. ./file.sh”? on Ask Ubuntu.

It appears that you wish to activate a python virtual environment where you installed tensorflow. You can try using an alias in ~/.bashrc .

alias pytensorflow='source ~/tensorflow/bin/activate' 

at the end of ~/.bashrc , below the line

# User specific aliases and functions 

Start a bash session and you can simply type in pytensorflow at the command prompt to activate the virtual environment. I have successfully used this.

Source files when opening an external terminal through Bash, Probably something like konsole -e ‘shell-of-choice -c «source whatever; command»‘ . – 41754. Oct 16, 2019 at 12:58. Add a comment |

Bash source file with some modification

I want to source a file in bash script . I know we can do —

. /path/to/file/file.cfg OR source /path/to/file/file.cfg 

But I am writing a single bash script in the world of Python scripts and need to use a config file created for Python scripts —

[section 1] param1=val1 param2=val2 [section 2] param1=val11 param2=val22 
source /path/to/python_config_file echo $param1 

/path/to/python_config_file: line 1: $'[section 1]\r’: command not found

I tried to remove the [ ] lines —

source `sed -e 's/\[[^][]*\]//g' /path/to/python_config_file` echo $param1 

It dumps all the variables as output —

source $’\r’ $’param1=val1\r’ $’param2=val2\r’ $’param1=val11\r’ $’param2=val22\r’

: No such file or directory + echo

How do I resolve this? I know I can create a config file for bash but the issue is that the client doesn’t want multiple config files as it’ll become maintenance nightmare plus python + bash configs have to be synced.

First, you need to remove the Windows-style endings from your file. If you don’t need to use the file on Windows, you can simply run dos2unix /path/to/file/file.cfg . Then, if you’re using bash , you can use process substitution:

This will let you source only those lines of file.cfg that contain a = , which should be exactly what you need.

If you can’t/don’t want to modify the file to remove the \r s, you can do thsi when reading it instead:

Include Files in a Bash Shell Script With source Command, The built-in bash source command reads and executes the content of a file. If the sourced file is a bash script, the overall effect comes

Источник

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