Linux - Newbie This 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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
03-09-2017, 06:14 PM
|
#1
|
Member
Registered: Nov 2015
Posts: 397
Rep: 
|
About /home/<user_name>/.bashrc and /etc/bashrc
From Centos 7, home/<user_name>/.bashrc :
Code:
# .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:
Why is the dot AND a space before /etc/bashrc needed?
Last edited by fanoflq; 03-19-2017 at 11:07 PM.
|
|
|
03-09-2017, 06:44 PM
|
#2
|
Moderator
Registered: Aug 2002
Posts: 26,901
|
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.
|
|
1 members found this post helpful.
|
03-09-2017, 07:20 PM
|
#3
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
Quote:
Originally Posted by michaelk
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
Code:
$ test=""
$ echo $test
$ . script1
-bash: ./myscript: Permission denied
$ ./script1
./script1: line 2: ./myscript: Permission denied
Why do I get these error messages?
What is the difference between
"./<some_script1>" and ". <some_script1>" ?
Last edited by fanoflq; 03-09-2017 at 07:25 PM.
|
|
|
03-09-2017, 07:27 PM
|
#4
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep: 
|
explains regular file testing
. means current directory
|
|
|
03-09-2017, 07:36 PM
|
#5
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
Quote:
Originally Posted by Habitual
explains regular file testing
. means current directory
|
Code:
Why do I get these error messages?
What is the difference between
"./<some_script1>" and ". <some_script1>" ?
|
|
|
03-09-2017, 07:38 PM
|
#6
|
Moderator
Registered: Aug 2002
Posts: 26,901
|
./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.
|
|
|
03-09-2017, 07:41 PM
|
#7
|
LQ Veteran
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Rep: 
|
|
|
|
03-09-2017, 07:55 PM
|
#8
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
michaelk & Habitual :
What I am still not able to understand is that
when I ran ./myscript, I got a permission denied error.
myscript is residing in the same directory as script1.
Quote:
~/dir $ test=""
~/dir $ ./myscript #running myscript inside its directory
-bash: ./myscript: Permission denied
~/dir $ . myscript
~/dir $ echo $test
Hello World!
|
Here is better question.
Why do i have to "chmod +x myscript" when I ran it like this:
Code:
~/dir $ ./myscript #running myscript inside its directory
while I do not have to "chmod +x myscript" for this command?
|
|
|
03-09-2017, 10:51 PM
|
#9
|
LQ Guru
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.x
Posts: 18,443
|
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
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.
HTH
Last edited by chrism01; 03-09-2017 at 10:54 PM.
|
|
1 members found this post helpful.
|
03-10-2017, 01:53 AM
|
#10
|
Member
Registered: Nov 2015
Posts: 397
Original Poster
Rep: 
|
chrism01:
Thank you.
Below for future reference....
Quote:
# 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.
|
|
|
03-10-2017, 07:30 AM
|
#11
|
LQ Guru
Registered: Sep 2013
Location: Somewhere in my head.
Distribution: Slackware (15 current), Slack15, Ubuntu studio, MX Linux, FreeBSD 13.1, WIn10
Posts: 10,342
|
this
and this
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.
. (source or dot operator)
|
|
|
All times are GMT -5. The time now is 04:32 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|