LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   _bash_profile (https://www.linuxquestions.org/questions/linux-newbie-8/_bash_profile-4175482147/)

Irina 10-25-2013 10:07 AM

_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

catkin 10-25-2013 10:49 AM

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


Irina 10-26-2013 07:55 AM

Quote:

Originally Posted by catkin (Post 5052204)
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:)

catkin 10-28-2013 12:30 AM

Quote:

Originally Posted by Irina (Post 5052656)
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*'


jv2112 10-28-2013 04:50 PM

_bash_profile
 
Place in your ~user/.bashrc

Next time you start a new session it will be there.

John VV 10-28-2013 05:49 PM

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

SAbhi 10-28-2013 11:23 PM

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.

Irina 11-04-2013 07:32 PM

Quote:

Originally Posted by John VV (Post 5054013)
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:)

catkin 11-06-2013 05:14 AM

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

Irina 11-06-2013 07:16 PM

Quote:

Originally Posted by catkin (Post 5059262)
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.

jv2112 11-09-2013 05:06 AM

Place it in your home/user/.bashrc


Code:


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


I placed in mine & tested and works fine.

catkin 11-10-2013 11:38 PM

Quote:

Originally Posted by Irina (Post 5059717)
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.

Irina 11-13-2013 07:29 PM

Quote:

Originally Posted by catkin (Post 5062245)
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:)


All times are GMT -5. The time now is 04:54 AM.