Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=
# User specific aliases and functions
~
I do not understand the bold faced lines above.
semantically it is like this:
If file /etc/bashrc exists,
then run this command:
Code:
. /etc/bashrc
Why is the dot AND a space before /etc/bashrc needed?
The dot is the source operator. In a nutshell it runs the script within the current shell and any variables created or modified by the script will remain available after the script completes. In your example /etc/bashrc is the system wide global definitions and if the file exists will be loaded in addition to the user definitions in the local bashrc file.
Another example.
script1
Code:
#!/bin/bash
. myscript
echo $test
myscript
Code:
#!/bin/bash
test="Hello World"
Make script1 executable and see what happens when you run it.
The dot is the source operator. In a nutshell it runs the script within the current shell and any variables created or modified by the script will remain available after the script completes. In your example /etc/bashrc is the system wide global definitions and if the file exists will be loaded in addition to the user definitions in the local bashrc file.
... ...
Thanks.
When I ran script1 like this: ./script1?
It works too.
BUT if I try this modification in script1, it fails:
Code:
#!/bin/bash
./myscript #added a forward slash
echo $test
./myscript will execute the script and since the permissions do not allow it you see the error message.
As stated in your example the . means current working directory. If you set the permissions for myscript it will execute in a subshell but script1 will display an error because test is undefined.
Because each invocation of 'bash' creates a new shell (+env).
Sourcing via either of
Code:
. script.sh
# OR
source script.sh
includes the contents of script.sh into your (already running) current shell/env.
(obviously you'll need r perms)
This
Code:
./script.sh
means run the script in the curr dir ( this is what './' means in this context) if a) it has rx perms and b) rx perms match curr owner and/or group (& strictly speaking the curr partition must be mounted with exec option; see /etc/fstab)
As above, a new shell is a sub-shell (you should google that) and vars+values cannot be 'exported' upwards ie when that shell completes, any var/values created/set within the sub-shell will be wiped.
# help .
#Notice the dot in front, i.e. help<space><dot>
#<dot> is a bash command when used like so.
.: . filename [arguments]
#Notice <dot><space><filename> [arguments]
Execute commands from a file in the current shell.
Read and execute commands from FILENAME in the current shell. The entries in $PATH are used to find the directory containing FILENAME.
If any ARGUMENTS are supplied, they become the positional parameters
when FILENAME is executed.
Exit Status:
Returns the status of the last command executed in FILENAME; fails if FILENAME cannot be read.
Likewise:
Quote:
# help source
#source is same as <dot> command See above.
source: source filename [arguments]
Execute commands from a file in the current shell.
... ...
./<filename> executes script in its own (sub)shell
and inherits from exported variables of current shell.
It does not do "reverse" exports of variables
back to the script that calls <filename> script.
are two completely different things. You should have noticed you changed it. as one stated "The dot is the source operator" by adding a forwards slash to it changes its meaning. It is like a function call that you added an unqualified data type to its parameters . It will no longer give you the same desired results.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.