vi .bash_profile function mkdircd - bash

# vi .bash_profile
function mkdircd () { mkdir -p "$#" && eval cd "\"\$$#\"";
}
I gave the above command in .bash_profile
But when I am typing mkdircd new_directory into the terminal I am getting following error
mkdircd: command not found

You may need to just reload your .bash_profile. Try this:
source ~/.bash_profile
Alternatively, you can save yourself a few keystrokes by typing this:
. ~/.bash_profile
The reason that you need to source the .bash_profile after you make changes to it is because the terminal window does not recognize these changes. You can also just open a new terminal window and your recent .bash_profile changes will be in effect in the new window.
If sourcing the .bash_profile still does not fix it, try changing your above code to this:
function mkdircd() { mkdir -p "$#" && cd "$#"; }

Related

zsh how to reload updated function definition

I am unable to update a function definition in my zsh dotfiles.
Specifically, I have this in my ~/.zshrc
unalias cd 2>/dev/null # Prevent infinite loops if sourcing this file again in the same session
logged_cd() {
cd "$#"
pwd > ~/.last_cd
}
alias "cd"="logged_cd" # keep track of most recent directory
I want to update the logged_cd function
logged_cd() {
cd "$#" && pwd > ~/.last_cd || return 1
}
So I updated my ~/.zshrc with the updated code, and did source ~/.zshrc but the old definition was still there.
I saw this one related question that suggested using unfunction which I tried, but that didn't work either.
Note that I also tried opening a new tab in my terminal so that zsh would be reloaded entirely but even in the new shell, it still is using the old function definition. Is something else going on here?

How to use function on git-prompt on Git Bash for Windows?

I want to use the last exit code to customize my bash prompt. So following this question I should add $? inside a function:
PROMPT_COMMAND=__prompt_command
__prompt_command() {
local EXIT="$?"
...
I tried many times, but whenever I add a function on my C:\Program Files\Git\etc\profile.d\git-prompt.sh file, Git Bash can't run this file, and it doesn't use it.
Is there a way to use a function on git-prompt.sh on Git Bash for Windows, or another approach to use the last exit code to customize the bash prompt?
Follow these instructions to configure your git-bash prompt.
First, open git-bash, and cd to your ~ directory.
Using vim or nano, edit your ~/.bash_profile file to contain the following two lines:
test -f ~/.profile && . ~/.profile
test -f ~/.bashrc && . ~/.bashrc
Then create a ~/.bashrc file and add your custom prompt command function in this file:
PROMPT_COMMAND=__prompt_command
__prompt_command() {
local EXIT="$?"
# The rest of your code here
}
Make sure your changes saved correctly for both your ~/.bash_profile and ~/.bashrc files.
Open a new git-bash window. Assuming you have no bugs in your __prompt_command() function, your custom prompt should display properly.
If you quickly want to test your prompt, you can instead just run source ~/.bashrc in your current git-bash window rather than launching a second one.

Why must I source .bashrc every time I open terminal for aliases to work?

Git was working fine. I have created an alias in Git but the issue is when I tried to reopen the terminal, then I need to run . ~/.bashrc every time in the terminal.
What is the best way I don't need to provide source every time when I reopen the terminal?
What I did?
I am trying to add source of the .bashrc file in this file but it is a read-only file. I am not able to add the source of the .bashrc file in this profile.
open /etc/profile
Added the permission to write in the profile as well, still not able to link the source file.
sudo chmod u+w /etc/profile
Profile:
# System-wide .profile for sh(1)
if [ -x /usr/libexec/path_helper ]; then
eval `/usr/libexec/path_helper -s`
fi
if [ "${BASH-no}" != "no" ]; then
[ -r /etc/bashrc ] && . /etc/bashrc
fi
It looks like your terminal emulator is launching bash as a login shell.
If that's the case, it will read /etc/profile for configuration as well as 1 of the following files, if they exist (listed in order of importance) :
~/.bash_profile
~/.bash_login
~/.profile
It will thus ignore your .bashrc file. A correct fix for your situation would be to either configure your terminal emulator to run bash interactively and non-login, or add the following line to your ~/.bash_profile :
[ -f "$HOME/.bashrc" ] && . "$HOME/.bashrc"
Here is a link to the documentation about which files are loaded depending of the type of shell you are running
As per #Aserre's answer i have followed this step to solve this issue
A typical install of OS won't create a .bash_profile for you. When you want to run functions from your command line, this is a must-have.
Start up Terminal
Type cd ~/ to go to your home folder
Type touch .bash_profile to create your new file.
Edit .bash_profile with your favorite editor (or you can just type open -e .bash_profile to open it in TextEdit.
[ -f "$HOME/.bashrc" ] && source "$HOME/.bashrc" Save it and close it
Restart the terminal, It should work
You should write this line source .profile inside your .zshrc file. This is because default shell is zsh. If u don't want to do this solution than u can go for changing the default shell by typing the following command chsh -s /bin/bash then restart your machine or virtual machine. Then no need for source. I hope this will help :) TAKE CARE
If you are using Linux and you want variables set, to persist.
Follow the below steps.
Be the root user -> sudo su
go to etc folder -> cd /etc
open the file bashrc with the editor of your choice -> vi bashrc
set the variable with export command like here I am setting JAVA_HOME ->
export JAVA_HOME=pathHere
Load the bashrc file with command ->
. bashrc
remember to put the dot/period before bashrc.
now JAVA_HOME should be set permanently.
Thanks...

alias script is not running `source ~/.bash_profile`

I have modified my .bash_profile script with the following command:
alias shortcuts-update='nano ~/.bash_profile && source ~/.bash_profile && say profile shortcuts have been updated'
It's a great way to add aliases on the fly. The nano works well but the the source ~/.bash_profile does not! I must run it manually before any changes I have saved in .bash_profile take effect. Can anyone tell me why?

Appending bash alias to .bashrc doesn't work

I want to create a alias of my cd command. I have created the .bashrc file and append the command cd ...... to it. (Since the file was newly created, it just has this one line that I added).
After that, only after I typed . ~/.bashrc, can the alias works. If I close the terminal and open it again, I need to retype . ~/.bashrc.
It's really annoying to do this every time. Is there any way to solve this problem?
Thank you so much for help
When you login to linux system, only ~/.profile will be called:
$ cat ~/.profile
# if running bash
if [ -n "$BASH_VERSION" ]; then
# include .bashrc if it exists
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
You need to source ~/.bashrc inside ~/.profile manaully.
Read this to learn more.
EDIT:
If you're using iTerm2 on mac, it actually start a login shell by default when open tabs.
But you can change it: Preferences > General > Command
If using OS X, append the alias to ~/.bash_profile.
You could also add alias to ~/.bashrc, then add source ~/.bashrc to ~/.bash_profile.
Better yet, put all your aliases in ~/.aliases, and source it in ~/.bash_profile.
By default, OS X first sources /etc/bashrc (which shouldn't be modified unless absolutely necessary), then sources the user's ~/.bash_profile at the start of every interactive session.

Resources