What is the difference between bash as a default shell and running 'bash'? - bash

I set up subl command in ~/bin
But I couldn't run the command subl unless I run bash in my terminal. I thought changing default shell from zsh to bash would fix it but it did not. I still have to run bash before subl and this is annoying.
What's the difference between default bash and command bash?
Why subl wouldn't work until I run bash and what should I do to make it work?
I've just started learning actual computer and I know these could be silly questions. Thanks a lot for your help.
TORIs-MacBook-Pro:~ taro$ echo $SHELL
/usr/local/bin/bash
TORIs-MacBook-Pro:~ taro$ subl --help
-bash: subl: command not found
TORIs-MacBook-Pro:~ taro$ bash
bash-5.0$ subl --help
Sublime Text build 3211

Execute ~/bin/subl
Your subl command located in ~/bin (very often equivalent to /home/user/bin) is probably not in zsh's PATH variable :
The command interpreter doesn't look everywhere on you computer when you execute a command, it has a few directories to search in. This list is stored in an environment variable called PATH. It contains something like this :
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
In my case, ~/bin is not in the list, but I can add it for this session only executing PATH="$PATH:~/bin" or include it when zsh starts by adding this line to ~/.zshrc :
PATH="$PATH:~/bin"
Now my PATH is :
$ echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:~/bin
Change the default shell
I'm not sure how it works with macOS X, but on Linux to change the default shell you need to execute this command line :
$ chsh -s /bin/bash
NOTE : the argument following -s must be a path to an interpreter, like bash.
You can found its path with which bash for example.
Then just relaunch your terminal or log out and in.
important : see first comment below about the tilde expansion (thanks to #Charles Duffy)

Related

Terminal not answering

I began noticing random bash: some_command: command not found on startup of bash so I tried switching to sh, but it also said bash: sh: command not found so I tried to restart bash and yet, bash: bash: command not found and I haven't changed any bash files. Please help...
Maybe the PATH variable is not set or empty?
Bash uses PATH environment variable to search for commands, unless an absolute path is used. It is a colon-separated list of directories in which the shell looks for commands: try man bash
You should try to display the list of directories where bash is looking for command with:
printf "%s\n" "${PATH}"
By default on my ubuntu 20.04 LTS, the above command output is:
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin

Assume cd when no command is specified and the first argument is a directory

Is it possible in bash to automatically cd into a directory even without typing cd?
E.g., I'd like to be able to cd into ./whatever when just typing the following into the terminal:
./whatever
Is there a way to achieve this with bash? I know that zsh supports this.
One of my thoughts was to catch the Is a directory error thrown by bash when just typing ./whatever and subsequently cd into that directory but I couldn't find out a way to do it.
Yes, just enable the shell option autocd:
shopt -s autocd
From the Bash Reference Manual:
autocd
If set, a command name that is the name of a directory is executed as if it were the argument to the cd command. This option is only used by interactive shells.

Aliases in .bash_profile not working properly [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
I have been trying to alter the .bash_profile that is in my root directory, but have been running into some problems. I am on OS X, Yosemite, on a Macbook Pro. As I understand it, the .bash_profile file contains the script that is called automatically whenever the Terminal app is opened and the bash shell starts.
This is what I currently have written in that file:
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
This works perfectly fine. However, I want to add an alias (right underneath the above two lines) as follows:
alias test='cd ..'
However, when I save this and start up the Terminal, I get the following message:
-bash: alias: ..": not found
Replacing the single quotes with double quotes doesn't help, nor does taking them away altogether. Curiously however, the following alias works:
alias c=clear
When I type c into the terminal, it clear the screen, as you would expect. However, if I instead entered this line with quotes in the bash profile as:
alias c='clear'
Then I will get the following whenever I enter c into the Terminal:
-bash: 'clear': command not found
Note that I do not get an error message on startup for this alias.
What am I doing wrong? Is there a setting I need to change somewhere to get aliases to work properly? I have seen previous examples of aliases and they simply do not work for me.
It looks like shell is not accessing your .bash_profile when logging via terminal.
.bash_profile is a config file of bash shell. When bash shell starting, shell will execute the commands in .bash_profile. But there are many kinds of shells, and different shells execute different config file.
Terminal is a software to receive user input, shell will execute commands.You can use cat /etc/shells to list acceptable shells. For example:
$ cat /etc/shells
/bin/bash
/bin/csh
/bin/ksh
/bin/sh
/bin/tcsh
/bin/zsh
The default shell is bash shell on Mac OX. But if you have installed zsh, the default shell is zsh, when zsh shell starting, shell will find out the file named .zshrc, and execute the commands in .zshrc.
You can use echo $SHELL to determine the current shell:
$ echo $SHELL
/bin/bash
-> echo $SHELL
/bin/zsh
If your default shell is zsh, .bash_profile don't work. The config file of zsh is .zshrc. And I guess your problem is that your default shell is not bash shell. For example, if your default shell is zsh, you should config the .zshrc , just add
PATH="/Library/Frameworks/Python.framework/Versions/2.7/bin:${PATH}"
export PATH
or other config to ~/.zshrc.
Then source ~/.zshrc, and the config will work immediately.
Wrapping the command with double quotation worked for me. I was trying with every possible way mentioned in this thread and none of them worked. Then I replaced single quote to double and that worked.
alias mysql_start="sudo /path/to/server/mysql.server start"
For me, it didn't work in the same terminal. I had to open a new one to get it work.
The killer for me was space and single quotes.
alias test="cd .." worked.
Don't put any spaces between alias_name=
User level 'system' files need to contain 'plain text'. How to set/configure this for your 'editor-of-choice' can vary (I don't use a Mac so I'm not much help with that.) Soo,
create your profile 'from the shell' by appending lines directly, i.e. remove the 'bad lines and then:
echo "some command string" >> ~/path/bash_profile
use 'vi' or 'vim' ## should be 'safe'
review your file via:
cat -v ~/path/bash_profle | more ## see any 'funny chars'? or
cat -ve ~/path/bash_profle | more ## see any 'funny chars'? or
or
vi ~/path/bash_profle # then set 'control codes' to 'on'
set list ## see any 'funny chars' for your 'quotes'?
What I realised is that Mac has option for smart quotes and dashes.
alias ..='cd ../' is different from alias ..=‘cd ../‘, where the former works but the latter doesn't.
You can run alias custom auto startup in ~/.bash_profile or ~/.alias_file... by paste file name alias in ~/.bashrc if you use bash or ~/.zshrc if you use zsh.
Ex:
if [ -s ~/.bash_profile ]; then
source ~/.bash_profile;
fi
I fixed this by editing my .bash_profile in vim or nano something about text edited messed it up unsure why.
I tried to edit in in notepad just using "open .bash_profile" however something about the symbols translated wrong. So you have to either "vim .bash_profile" or "nano .bash_profile"
Doing this corrected this problem for me.
Make sure that the alias commands are together in the .bash_profile too. That is what was wrong with mine.
Also, if you just type in alias in the terminal, it should list all the known alias commands, so if you don't see your command you know something is wrong.
If alias is not recognizing, first identify what shell you're using when you open up your terminal or commandlineprompt echo $SHELL. For me it's /bin/bash so i'll input my aliases in ~/.bash_profile
Take note, the file ~/.bash_profile can store both aliases to call out during a terminal session and autorun those same aliases upon opening up a terminal session. The example below would be to open up a file to edit by text via vim-software. You may change it other text-editors as you wish. You can see the difference by closure-method.
ALIAS CALLOUT (uses parenthesis)
alias editbp="vim ~/.bash_profile"
ALIAS AUTORUN (uses tilda)
alias editbp=`vim ~/.bash_profile`

How to make OS X to read .bash_profile not .profile file

I have read so many suggestions about, not putting your customization aka commands in ".profile" file. Rather, create a .bash_profile for yourself and add your alias and etc.
But,when I open the new terminal, if there is only .bash_profile, OS X is not exporting/sourcing the commands mentioned in it. I have to manually source the .bash_profile.
If I create .profile file, on opening a new terminal, all my commands in .profile are executed and will be available readily.
Could you please help me in understanding, how does it works? Also, when to use .bashrc/.profile/.bash_profile files.
Thanks!
According to Apple,
zsh (Z shell) is the default shell for all newly created user accounts, starting with macOS Catalina.
So you should verify your default shell with the command:
$ echo $SHELL
If the result is /bin/bash your default shell is BASH, and if the result is /bin/zsh the default is ZSH.
Go to home with $ cd ~/ and create the profile (if it does not exist) and edit it with the commands:
For bash:
$ touch .bash_profile
$ open .bash_profile
For ZSH:
$ touch .zprofile
$ open .zprofile
According to the manual page that ships with OS X:
... it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile, in that order, and reads and executes commands from the first one that exists and is readable. The --noprofile option may be used when the shell is started to inhibit this behavior.
It should only read ~/.profile as a last resort if neither ~/.bash_profile nor ~/.bash_login are readable.
On all of my OS X systems, I have my ~/.bash_profile set to:
if [ -f ~/.bashrc ]; then
source ~/.bashrc
fi
It is highly recommended that you do this on OS X in order to get bash to read your ~/.bashrc file like you would expect.
It's also possible that your terminal shell is defaulting to sh instead of bash. You can verify this first:
$ echo $SHELL
/bin/tcsh
To change this to bash, you can go into your Terminal -> Preferences -> Startup tab, and change "Shell Opens With:" from "Default login shell" to Command and value "/bin/bash".
Alternately, you can change your default shell by executing the following command at the command prompt:
chsh -s bin/bash
After you do one of these, open a new shell window, and your .bash_profile should be sourced.
For anyone else who finds this, instead of bash_profile, for new versions of mac you can use .zshrc. I.E., do
open .zshrc
and add what you need there.
You can use zsh to fix the problem.
The Z shell (also known as zsh) is a Unix shell that is built on top
of bash (the default shell for macOS) with additional features. It's
recommended to use zsh over bash.
Installation
Install zsh using Homebrew: $ brew install zsh
Install Oh My Zsh: $ sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"
Move to .bash_profile setting .zshrc file
To apply the changes you make you need to either start new shell
instance or run: source ~/.zshrc
If you are using zsh, you can source to .bash_profile by adding the following line to .zprofile
if [ -f ~/.bash_profile ]; then
source ~/.bash_profile
fi
It should be mentioned that bash will first look for a /etc/profile file, as stated in the Bash man pages.
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, ~/.bash_login, and ~/.profile,
in that order, and reads and executes commands from the first one that
exists and is readable. The --noprofile option may be used when the
shell is started to inhibit this behavior.
I solved by simply adding bash (in a newline) into ~/.bash_profile file.

bash shell $HOME assignment and script execution

I've just begun learning Unix and have so far encountered two elementary though difficult to resolve problems:
When I set HOME='' in a shell script to a designated directory, the current directory no longer seems to be recognized. That is, 'cd ~/' spits out the message: 'no such file or directory' message. Although, curiously enough, if aliases assignments are made within the script, a source call seems to activated them nonetheless. How come?
Ex:
$ more .profile
HOME="~/Documents/Basics/Unix/Unix_and_Perl_course"
cd $HOME
[...]
$ source .profile
-bash: cd: ~/Documents/Basics/Unix/Unix_and_Perl_course: No such file or directory
When I created a simple shell script via nano ('hello.sh'), I can't seem to execute it simply by typing 'hello.sh' in the terminal. This issue fails to resolve even after I 'chmod +x' the file. What's the problem?
Ex:
$ more hello.sh
# my first Unix shell script
echo "Hello World"
$ hello.sh
bash: hello.sh: command not found
Thanks!
You also don't want to 'overload' $HOME, the default location for HOME is always your home directory. If you goof with that, lots of things will break.
As far as hello.sh - thats because you don't have '.' in your $PATH. (Which is a good thing)
Try:
./hello.sh
If it says it can't execute
chmod 755 hello.sh
./hello.sh
~ = $HOME
. (pwd) is not in $PATH

Resources