LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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


Reply
  Search this Thread
Old 03-09-2017, 06:14 PM   #1
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Rep: Reputation: Disabled
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:
Code:
. /etc/bashrc
Why is the dot AND a space before /etc/bashrc needed?

Last edited by fanoflq; 03-19-2017 at 11:07 PM.
 
Old 03-09-2017, 06:44 PM   #2
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,380

Rep: Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469
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.
Old 03-09-2017, 07:20 PM   #3
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by michaelk View Post
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.
 
Old 03-09-2017, 07:27 PM   #4
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Code:
man test
explains regular file testing

. means current directory
 
Old 03-09-2017, 07:36 PM   #5
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Habitual View Post
Code:
man test
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>" ?
 
Old 03-09-2017, 07:38 PM   #6
michaelk
Moderator
 
Registered: Aug 2002
Posts: 24,380

Rep: Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469Reputation: 5469
./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.
 
Old 03-09-2017, 07:41 PM   #7
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
http://www.linuxquestions.org/questi...-paths-256350/
 
Old 03-09-2017, 07:55 PM   #8
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
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?
Code:
~/dir $ . myscript
 
Old 03-09-2017, 10:51 PM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 7.7 (?), Centos 8.1
Posts: 18,238

Rep: Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712Reputation: 2712
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.


HTH

Last edited by chrism01; 03-09-2017 at 10:54 PM.
 
1 members found this post helpful.
Old 03-10-2017, 01:53 AM   #10
fanoflq
Member
 
Registered: Nov 2015
Posts: 397

Original Poster
Rep: Reputation: Disabled
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.
 
Old 03-10-2017, 07:30 AM   #11
BW-userx
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,248

Rep: Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205Reputation: 2205
this
Code:
. /etc/bashrc
and this
Code:
./script.sh
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)
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] /etc/bashrc ,dircolors and /root/.bash_profile ,/.bashrc not exist in LFS-7.5(sec9.3) jaassi Linux From Scratch 2 09-20-2014 05:04 PM
~/.bashrc, /etc/bash.bashrc files not read? Tachtory Slackware 3 01-04-2014 12:25 AM
[SOLVED] How to use my /home/kangjoo/.bashrc rather than root/.bashrc kangjoo.lee Linux - Newbie 2 11-05-2012 03:38 PM
.bashrc / .bash_profile /etc/profile /etc/bashrc deadeyes Red Hat 2 02-13-2010 11:22 AM
Setting path: /etc/profile, /etc/bashrc or ~/.bashrc Swakoo Linux - General 1 08-07-2007 10:59 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 08:39 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration