Linux - NewbieThis 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.
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.
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
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
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
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
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
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.
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
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.
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.
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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.