LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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 10-25-2013, 10:07 AM   #1
Irina
LQ Newbie
 
Registered: Oct 2013
Posts: 5

Rep: Reputation: Disabled
Smile _bash_profile


Good Afternoon,
I placed mkdircd() function in _bash_profile and saved using VI editor.
# vi .bash_profile
function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\"";}
I expected to use it next day I logged on. But it did not recognize this function.
I read that _bash_profile is hidden file and ctl+H will be able to view it but I cannot view the contents of the file. Sorry, where bash_profile is? what purpose does it serve? Please answer when you can. Thank you Irina
 
Old 10-25-2013, 10:49 AM   #2
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
It's .bash_profile (not _bash_profile or bash_profile) and it must be in your home directory (~) to work.

If you logged on at a graphical logon ("greeter") screen, ~/.bash_profile may not have been run; not all of the graphical logon programs run ~/.bash_profile.

You can test whether ~/.bash_profile is creating the mkdircd function like this:
Code:
source ~/.bash_profile
declare -f

Last edited by catkin; 10-25-2013 at 10:51 AM. Reason: It makes sense now
 
1 members found this post helpful.
Old 10-26-2013, 07:55 AM   #3
Irina
LQ Newbie
 
Registered: Oct 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
It's .bash_profile (not _bash_profile or bash_profile) and it must be in your home directory (~) to work.

If you logged on at a graphical logon ("greeter") screen, ~/.bash_profile may not have been run; not all of the graphical logon programs run ~/.bash_profile.

You can test whether ~/.bash_profile is creating the mkdircd function like this:
Code:
source ~/.bash_profile
declare -f
Thank you very much for your prompt reply.
I believe I cannot see my .bash_profile. I tried the following:
source ~/.bash_profile
declare -f from my username/localhost command line in terminal screen. However if I try from this command to do again vi .bash_profie the editor is open .bash_profile as a new file but I cannot see what I saved before ( my mkdircd function ).I will reserach more. Somewhere I am wrong. Thank you for patience. I really enjoy this help line. Fells like our beutiful world is so small
 
Old 10-28-2013, 12:30 AM   #4
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by Irina View Post
Thank you very much for your prompt reply.
I believe I cannot see my .bash_profile. I tried the following:
source ~/.bash_profile
declare -f from my username/localhost command line in terminal screen. However if I try from this command to do again vi .bash_profie the editor is open .bash_profile as a new file but I cannot see what I saved before ( my mkdircd function ).I will reserach more. Somewhere I am wrong. Thank you for patience. I really enjoy this help line. Fells like our beutiful world is so small
When you ran source ~/.bash_profile was there any output? If the ~/.bash_profile file does not exist, you should have seen bash: /home/<your_username>/.bash_profile: No such file or directory.

Assuming you did see that, either you saved .bash_profile in a different directory or under a different name (or you did not save it). Assuming the name contains _profile and you saved it under your home directory, you could find it by
Code:
cd
find -type f -iname '*_profile*'
 
Old 10-28-2013, 04:50 PM   #5
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 106Reputation: 106
_bash_profile

Place in your ~user/.bashrc

Next time you start a new session it will be there.
 
Old 10-28-2013, 05:49 PM   #6
John VV
LQ Muse
 
Registered: Aug 2005
Location: A2 area Mi.
Posts: 17,627

Rep: Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651Reputation: 2651
the VI text editor for the terminal is not "new linux user friendly"
how exactly did you save the text file in VI ( should save on exit)
and what mode were you in
and what commands did you use to close out vi


for small edits i normally use "nano" it is a much more user friendly editor
and perfect for minor edits

but if you have a desktop gui installed "gedit" or " kate" are the default editors

Last edited by John VV; 10-28-2013 at 05:51 PM.
 
Old 10-28-2013, 11:23 PM   #7
SAbhi
Member
 
Registered: Aug 2009
Location: Bangaluru, India
Distribution: CentOS 6.5, SuSE SLED/ SLES 10.2 SP2 /11.2, Fedora 11/16
Posts: 665

Rep: Reputation: Disabled
why we use .bash_profile:

my PATH set up into a .profile file (because I sometimes use other shells)
my bash aliases and functions into my .bashrc
Then use this:
.bash_profile:

simply:

According to the bash man page, .bash_profile is executed for login shells.When you login (type username and password) via console, either sitting at the machine, or remotely via ssh: .bash_profile is executed to configure your shell before the initial command prompt.

Last edited by SAbhi; 10-28-2013 at 11:29 PM.
 
Old 11-04-2013, 07:32 PM   #8
Irina
LQ Newbie
 
Registered: Oct 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by John VV View Post
the VI text editor for the terminal is not "new linux user friendly"
how exactly did you save the text file in VI ( should save on exit)
and what mode were you in
and what commands did you use to close out vi


for small edits i normally use "nano" it is a much more user friendly editor
and perfect for minor edits

but if you have a desktop gui installed "gedit" or " kate" are the default editors
VI editor is ok. I used "a" for append mode and able to insert function. Then I used "esc" to escape and saved :X
which broght me back to user/localhost. Function find is great it showed all my directories and files.
I can see .bashrc and see my new saved function there. Now if I use it it creates directory but does not automatically redirect to it. Is it because I am in user/localhost propmt? Thank you
 
Old 11-06-2013, 05:14 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
That's strange -- it works for me:
Code:
c@CW8:/tmp$ function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\"";}
c@CW8:/tmp$ mkdircd foo
c@CW8:/tmp/foo$
Can you try it with set -xv and copy the output here in code tags like:
Code:
c@CW8:/tmp/foo$ set -xv
c@CW8:/tmp/foo$ mkdircd bar
mkdircd bar
+ mkdircd bar
+ mkdir -p bar
+ eval cd '"$1"'
cd "$1"
++ cd bar
c@CW8:/tmp/foo/bar$
The easy way to get code tags is use "Advanced mode" and use the # button.

To turn off the set -xv use
Code:
set +xv
 
Old 11-06-2013, 07:16 PM   #10
Irina
LQ Newbie
 
Registered: Oct 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
That's strange -- it works for me:
Code:
c@CW8:/tmp$ function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\"";}
c@CW8:/tmp$ mkdircd foo
c@CW8:/tmp/foo$
Can you try it with set -xv and copy the output here in code tags like:
Code:
c@CW8:/tmp/foo$ set -xv
c@CW8:/tmp/foo$ mkdircd bar
mkdircd bar
+ mkdircd bar
+ mkdir -p bar
+ eval cd '"$1"'
cd "$1"
++ cd bar
c@CW8:/tmp/foo/bar$
The easy way to get code tags is use "Advanced mode" and use the # button.

To turn off the set -xv use
Code:
set +xv
Hello. Thank you very much. I tried your code. It ctreates and redirect to new directoty but only for the duration of my login. When I exit and come back it does not know this function. My idea is to store this function in .bashrc file so I can use it every time without retyping it.
.bashrc which I access from user@localhost command line via VI editor contains my function mkdircd ().
It is getting intersting... now when i type mkdircd lala it said bash : mkdircd : command not found. "Find" command shows that I have ./bashrc and ./.bashrc files.
 
Old 11-09-2013, 05:06 AM   #11
jv2112
Member
 
Registered: Jan 2009
Location: New England
Distribution: Arch Linux
Posts: 719

Rep: Reputation: 106Reputation: 106
Lightbulb

Place it in your home/user/.bashrc


Code:
function mkdircd () { mkdir -p "$@" && eval cd "\"\$$#\"";}

I placed in mine & tested and works fine.
 
Old 11-10-2013, 11:38 PM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Debian
Posts: 8,578
Blog Entries: 31

Rep: Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208Reputation: 1208
Quote:
Originally Posted by Irina View Post
Hello. Thank you very much. I tried your code. It ctreates and redirect to new directoty but only for the duration of my login. When I exit and come back it does not know this function. My idea is to store this function in .bashrc file so I can use it every time without retyping it.
.bashrc which I access from user@localhost command line via VI editor contains my function mkdircd ().
It is getting intersting... now when i type mkdircd lala it said bash : mkdircd : command not found. "Find" command shows that I have ./bashrc and ./.bashrc files.
The issue could be the difference between a "login" shell and an an "interactive non-login" shell. More information: http://www.gnu.org/software/bash/man...-Startup-Files.

This could be tested by logging in and running ...
Code:
source ~/.bashrc
... and then try to run the function.

A login bash shell uses /etc/profile and the first of ~/.bash_profile, ~/.bash_login and ~/.profile that exists.

An "interactive non-login" bash shell uses ~/.bashrc.

So, if the function definition is in ~/.bashrc and you login to bash the function is not available.

A common technique to have the same customisation regardless of login or non-login, is to source ~/.bashrc in ~/.bash_profile:
Code:
[[ -f ~/.bashrc ]] && source ~/.bashrc
Your ~/bashrc file will not automatically be used by bash so you may like to remove it to avoid confusion.

Last edited by catkin; 11-10-2013 at 11:39 PM.
 
Old 11-13-2013, 07:29 PM   #13
Irina
LQ Newbie
 
Registered: Oct 2013
Posts: 5

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by catkin View Post
The issue could be the difference between a "login" shell and an an "interactive non-login" shell. More information: http://www.gnu.org/software/bash/man...-Startup-Files.

This could be tested by logging in and running ...
Code:
source ~/.bashrc
... and then try to run the function.

A login bash shell uses /etc/profile and the first of ~/.bash_profile, ~/.bash_login and ~/.profile that exists.

An "interactive non-login" bash shell uses ~/.bashrc.

So, if the function definition is in ~/.bashrc and you login to bash the function is not available.

A common technique to have the same customisation regardless of login or non-login, is to source ~/.bashrc in ~/.bash_profile:
Code:
[[ -f ~/.bashrc ]] && source ~/.bashrc
Your ~/bashrc file will not automatically be used by bash so you may like to remove it to avoid confusion.

Perfect! Thank you very much! I tried code :source ~/.bashrc and then used my function mkdircd perfectly. I also updated .bash_profile to "include" functions from .bashrc and ran my function mkdircd succesfully. And what I relly liked to accompllish is to exit the terminal and login back and the function works! Thank you so much! You are great! Excellent! The whole idea to help each other is amazing! I hope I can help someone in the future
 
  


Reply



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



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

All times are GMT -5. The time now is 08:10 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