Terminal not answering - bash

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

Related

How to let Bash script extends User's PATH?

At command line, my command python3 -u jupterlab notebook works perfectly as python locates at /srv/conda/envs/notebook/bin/python3.
Next, I have a bash script, say /usr/local/share/python3-login, its content is
#!/bin/bash -l
echo $PATH
exec python3 -u "$#"
My problem is when I call the script, I encountered an error where python3 not found /usr/local/share/python3-login: line 3: exec: python3: not found
I tried to debug by adding echo $PATH at line 2, and turned out PATH is /opt/conda/condabin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/game, which python3 doesn't exist
How to let my bash script recognize /srv/conda/envs/notebook/bin/python3?
To add more context, I built a docker image with Ubuntu OS
You can just use the full path:
#!/bin/bash -l
echo $PATH
exec /srv/conda/envs/notebook/bin/python3 -u "$#"
To extend the user's path, modify the PATH environment variable.
export PATH=/srv/conda/envs/notebook/bin/python3:$PATH
-l Make bash act as if it had been invoked as a login shell
That means it will loads the various profile files as if you just logged in
Since /srv/conda/envs/notebook/bin does not look as a standard path one would see in an profile file, I suspect you do something more to get this in your $PATH in the first place.
solution 1: simply add whatever you do to get this path in your environment, into the script.
solution 2: simply don't use the -l argument in your shebang.

What is the difference between bash as a default shell and running '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)

“export: command not found [duplicate]

This question already has an answer here:
How to restore .bash_profile on a mac? None of my unix terminal are working [closed]
(1 answer)
Closed 2 years ago.
When I open terminal on my mac it shows
Last login: Sun Mar 15 22:12:02 on ttys000
-bash: “export: command not found
-bash: “export: command not found
-bash: “export: command not found
-bash: “export: command not found
(My echo $PATH)
MacBook-Air-Tim:~ timreznik$ echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin:/usr/local/git/bin:/Users/timreznik/bin:/usr/local/bin
MacBook-Air-Tim:~ timreznik$
I have already tried to edit my .bash_profile to
# general path munging
PATH=${PATH}:~/bin
PATH=${PATH}:/usr/local/bin
but it still keep showing me “export: command not found when I launch terminal...
P.S. all commands seems to work but my inner perfectionist is screaming!
First, export is a shell builtin:
$ type export
export is a shell builtin
This means that PATH is irrelevant.
Second, the error message makes clear that the script is attempting to run the command “export. There is no such command:
$ “export
bash: $'\342\200\234export': command not found
The solution is to remove the spurious character from before the string export.
This misspelled command is in one of the shell's initialization files. These would include: ~/.bashrc, /etc/bash.bashrc, ~/.bash_profile, ~/.bash_login, ~/.profile, and any files they include.
Alternatively, the following commands will tell you which files and which lines in those files have the misspelled export command:
PS4='+ $BASH_SOURCE:$LINENO:' BASH_XTRACEFD=7 bash -xlic "" 7>trace.out
grep '“export' trace.out
For details on how the above works, see this post.
I had a similar problem, the culprit was non-breaking space between export and the name of the variable.
To resolve the issue, just replace it with a regular space character.
Details:
I had the following in .bash_profile:
export a=foo
When I start new terminal, I would get
-bash: export a=foo: command not found
If we run xxd on the file, however, we can plainly see the problem (dots are non-printable characters:
$ cat .bash_profile | head -n1 | xxd
00000000: 6578 706f 7274 c2a0 613d 666f 6f export..a=foo
Byte sequence c2a0 stands for non-breaking space

Unable to successfully execute some commands in Cygwin

I installed Cygwin64 in my 64-bit Windows 7 machine. The following commands failed executing, however, by displaying the error messages below. Could you help providing a resolution please?
$ ll
-bash: ll: command not found
$ clear
-bash: clear: command not found
However, the command ls -l worked...
$ ls -l
total 0
Also i tried by un-commenting the following line in .bashrc file in my home dir -
# alias ll='ls -l'
But it didn't help either!
After you uncomment the alias, you should start a new Cygwin shell for it to take effect. The .bashrc file is actually a script that is sourced when bash starts.
clear is not a Cygwin (Unix) command. Just use Ctrl-L instead.

difference in execution of a script in bash and korn

i have a script that reads a file line by line
the code is
FILE=commands.txt
while read CMD; do
echo "$CMD"
done < "$FILE"
This code is stored in a script file vxm_alarm.sh
In Korn shell, this loop works perfectly, when i run the command vxm_alarm.sh. In bash however i get the following error
vxm_alarm.sh: syntax error at line 4: `done' unexpected
In Bash I'm executing the script using the command sh vxm_alarm.sh. what am i doing wrong? And why can't we execute a script simply by doing this in bash
chmod +x filename.sh
filename.sh
Your code works on my machine using GNU bash 4.1.5
Try adding a shebang to the top:
#!/bin/sh
FILE=commands.txt
while read CMD; do
echo "$CMD"
done < "$FILE"
If you run sh vxm_alarm.sh you are most likely not running Bash. Try sh --version - If you get anything other than a version string, it's not Bash. Try running bash vxm_alarm.sh instead.
To be able to run a script without a path before it it has to be in one of the directories listed in the PATH variable. For example, if
echo "$PATH"
prints
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
you can put filename.sh in /usr/local/sbin, /usr/local/bin, /usr/sbin, /usr/bin, /sbin or /bin and run it as simply filename.sh. If you want your script to be run from a directory not in the path, you have three choices:
Modify $PATH to include the directory where the script resides.
Run it with a relative or absolute path.
Create an alias or function pointing to the relative or absolute path.
I'd like to answer this part of your question:
why can't we execute a script simply by doing this in bash
chmod +x filename.sh
filename.sh
As others already pointed out in part, there are several things required for that to work:
execution rights (You ensured that with your chmod command)
the shebang, so the system knows what shell/interpreter to use
#!/bin/bash
(it is important to say bash if you want bash and not sh)
make sure the command is found. This is the case when its directory is found in PATH. However what you'd rather do in this case is specify the directory. For the current directory you can do it like this
./name-of-the-script
In contrast to DOS and (IIRC) the various Windows Command line interfaces, Unix systems usually don't have the current directory on the PATH. It is possible to add it, but discouraged due to severe implications on security.

Resources