LinuxQuestions.org
Review your favorite Linux distribution.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Linux From Scratch
User Name
Password
Linux From Scratch This Forum is for the discussion of LFS.
LFS is a project that provides you with the steps necessary to build your own custom Linux system.

Notices


Reply
  Search this Thread
Old 08-01-2012, 10:22 PM   #1
greatdictator
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Rep: Reputation: Disabled
Question about bash_profile script ?


Hi,

I'm new to LFS project and want to learn as much as possible.I'm wondering what is the point of setting bash_profile script
Code:
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
to set up clean environment if that script is only executed when I log into the system.I mean, every time I open new shell in terminal emulator, ENV variables will be created from the system-wide bash configuration files, so, what's the point? Wouldn't it be better if that code is in bashrc ?
 
Old 08-02-2012, 12:13 AM   #2
sag47
Senior Member
 
Registered: Sep 2009
Location: Raleigh, NC
Distribution: Kubuntu x64, Raspbian, CentOS
Posts: 1,861
Blog Entries: 36

Rep: Reputation: 459Reputation: 459Reputation: 459Reputation: 459Reputation: 459
type exec is a shell built-in for bash. From the bash man page,

Code:
SHELL BUILTIN COMMANDS
...
       exec [-cl] [-a name] [command [arguments]]
              If  command is specified, it replaces the shell.  No new process
              is created.  The arguments become the arguments to command.   If
              the -l option is supplied, the shell places a dash at the beginā
              ning of the zeroth argument passed to  command.   This  is  what
              login(1) does.  The -c option causes command to be executed with
              an empty environment.  If -a is supplied, the shell passes  name
              as the zeroth argument to the executed command.  If command canā
              not be executed for some reason, a non-interactive shell  exits,
              unless  the  shell  option execfail is enabled, in which case it
              returns failure.  An interactive shell returns  failure  if  the
              file cannot be executed.  If command is not specified, any rediā
              rections take effect in the current shell, and the return status
              is 0.  If there is a redirection error, the return status is 1.
...
type env is /bin/env in my system. From the env man page for the -i option.
Code:
NAME
       env - run a program in a modified environment

SYNOPSIS
       env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]
...
       -i, --ignore-environment
              start with an empty environment
...
Explanation
The TERM and HOME environment variables are specified because a new empty environment will be created without those variables. Therefore they should be set using the current environment variables.

Why is it doing that?

Essentially what it is doing is setting the format for how the environment is displayed for the user. For instance when user sam logs in then his environment would look like the following.
Code:
sam:~$
For instance on my system my PS1="[\u@\h \W]\$ " therefore my environment looks like the following (note sam is my user and stealth is my hostname).
Code:
[sam@stealth ~]$
It's just a bit of voo-doo magic to get the format of the prompt to display however the user wishes. Other than that it doesn't serve much purpose.

SAM

Last edited by sag47; 08-02-2012 at 12:16 AM.
 
Old 08-02-2012, 06:35 AM   #3
greatdictator
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by sag47 View Post
type exec is a shell built-in for bash. From the bash man page,

Code:
SHELL BUILTIN COMMANDS
...
       exec [-cl] [-a name] [command [arguments]]
              If  command is specified, it replaces the shell.  No new process
              is created.  The arguments become the arguments to command.   If
              the -l option is supplied, the shell places a dash at the beginā
              ning of the zeroth argument passed to  command.   This  is  what
              login(1) does.  The -c option causes command to be executed with
              an empty environment.  If -a is supplied, the shell passes  name
              as the zeroth argument to the executed command.  If command canā
              not be executed for some reason, a non-interactive shell  exits,
              unless  the  shell  option execfail is enabled, in which case it
              returns failure.  An interactive shell returns  failure  if  the
              file cannot be executed.  If command is not specified, any rediā
              rections take effect in the current shell, and the return status
              is 0.  If there is a redirection error, the return status is 1.
...
type env is /bin/env in my system. From the env man page for the -i option.
Code:
NAME
       env - run a program in a modified environment

SYNOPSIS
       env [OPTION]... [-] [NAME=VALUE]... [COMMAND [ARG]...]
...
       -i, --ignore-environment
              start with an empty environment
...
Explanation
The TERM and HOME environment variables are specified because a new empty environment will be created without those variables. Therefore they should be set using the current environment variables.

Why is it doing that?

Essentially what it is doing is setting the format for how the environment is displayed for the user. For instance when user sam logs in then his environment would look like the following.
Code:
sam:~$
For instance on my system my PS1="[\u@\h \W]\$ " therefore my environment looks like the following (note sam is my user and stealth is my hostname).
Code:
[sam@stealth ~]$
It's just a bit of voo-doo magic to get the format of the prompt to display however the user wishes. Other than that it doesn't serve much purpose.

SAM
Hi Sam,
thank you for trying to explain these thing to me.
I perfectly understand what you say, but my real question is this - Yes, I will start with clean environment after log in, HOWEVER, environment will be clean as long as I don't start new non-login shell.In that new shell, environment variables will be set from some other configuration file(not sure where from), so that kind of defeat the purpose of bash_profile script.For example, when I log in, and then start new shell where I will build the system, I get this env vars set(my host system is ubuntu)
Code:
XDG_MENU_PREFIX=lxde-
XDG_SESSION_COOKIE=cad870f65f7ec47808d6744000000006-1343766032.763376-1423612250
GNOME_KEYRING_CONTROL=/tmp/keyring-zXcnq3
GTK_MODULES=canberra-gtk-module:canberra-gtk-module
That doesn't look like a clean env to me...
 
Old 08-02-2012, 07:41 AM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401Reputation: 2401
Quote:
Originally Posted by greatdictator View Post
I'm wondering what is the point of setting bash_profile script
Code:
exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash
to set up clean environment if that script is only executed when I log into the system.
I mean, every time I open new shell in terminal emulator, ENV variables will be created from the system-wide bash configuration files, so, what's the point? Wouldn't it be better if that code is in bashrc ?
This specific ~/.bash_profile is executed when you log in as user lfs (i.e.: su - lfs)
Quote:
When bash is invoked as an interactive login shell, or as a non-inter‐
active shell with the --login option, it first reads and executes com‐
mands from the file /etc/profile, if that file exists. After reading
that file, it looks for ~/.bash_profile
So after you issue su - lfs, /etc/profile is parsed first and then ~/.bash_profile. Because .bash_profile contains exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash, all that is set by /etc/profile is removed (the env -i part and a few basic things are set (HOME, TERM and PS1). The exec line ends with /bin/bash, which starts a non-login shell and .bashrc is read, which sets a few other environment variables. This process ensures a clean environment.

Code:
[plains] druuna ~ $ env
ORBIT_SOCKETDIR=/tmp/orbit-druuna
SSH_AGENT_PID=3615
.
.
.
WINDOWPATH=7
DISPLAY=:0.0
LC_TIME=nl_NL.utf8
XAUTHORITY=/var/run/gdm3/auth-for-druuna-jcgq0i/database
_=/usr/bin/env
OLDPWD=/home/druuna
[plains] druuna ~ $ su - lfs
Password: 
[plains] lfs ~ $ env
TERM=xterm
LC_ALL=POSIX
LFS=/mnt/lfs
PATH=/tools/bin:/bin:/usr/bin
PWD=/home/lfs
LFS_TGT=x86_64-lfs-linux-gnu
PS1=\[\033[34m\][`uname -n`] \u \[\033[0m\]\w $ 
SHLVL=1
HOME=/home/lfs
_=/usr/bin/env
You do need to use su - lfs and not su lfs!!The latter will start a none login shell that does not read ~/.bash_profile, it reads ~/.bashrc and thus doesn't clean the environment.
 
1 members found this post helpful.
Old 08-02-2012, 07:57 AM   #5
greatdictator
LQ Newbie
 
Registered: Feb 2012
Posts: 6

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by druuna View Post
This specific ~/.bash_profile is executed when you log in as user lfs (i.e.: su - lfs)
So after you issue su - lfs, /etc/profile is parsed first and then ~/.bash_profile. Because .bash_profile contains exec env -i HOME=$HOME TERM=$TERM PS1='\u:\w\$ ' /bin/bash, all that is set by /etc/profile is removed (the env -i part and a few basic things are set (HOME, TERM and PS1). The exec line ends with /bin/bash, which starts a non-login shell and .bashrc is read, which sets a few other environment variables. This process ensures a clean environment.

Code:
[plains] druuna ~ $ env
ORBIT_SOCKETDIR=/tmp/orbit-druuna
SSH_AGENT_PID=3615
.
.
.
WINDOWPATH=7
DISPLAY=:0.0
LC_TIME=nl_NL.utf8
XAUTHORITY=/var/run/gdm3/auth-for-druuna-jcgq0i/database
_=/usr/bin/env
OLDPWD=/home/druuna
[plains] druuna ~ $ su - lfs
Password: 
[plains] lfs ~ $ env
TERM=xterm
LC_ALL=POSIX
LFS=/mnt/lfs
PATH=/tools/bin:/bin:/usr/bin
PWD=/home/lfs
LFS_TGT=x86_64-lfs-linux-gnu
PS1=\[\033[34m\][`uname -n`] \u \[\033[0m\]\w $ 
SHLVL=1
HOME=/home/lfs
_=/usr/bin/env
You do need to use su - lfs and not su lfs!!The latter will start a none login shell that does not read ~/.bash_profile, it reads ~/.bashrc and thus doesn't clean the environment.
Now I understand, thanks!
 
  


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
Difference between running script (.sh) from .bash_profile and running from login dsimha Linux - General 4 01-17-2012 11:06 AM
.bash_profile question Gald3r Linux - Newbie 8 09-20-2005 08:17 PM
how to Run python script on .bash_profile warnetgm Linux - Newbie 32 09-08-2005 06:56 AM
.bash_profile question jalburger Linux - Newbie 1 03-18-2003 05:42 PM
.bash_profile question abyss Linux - General 7 03-19-2002 12:04 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Linux From Scratch

All times are GMT -5. The time now is 03:30 PM.

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