zsh: command not found - only works when I change path, but on restarting terminal, path changes back - bash

I have 4 files in my bin. Funnily, two of them work when I call them in the terminal - the other (newer) two don't.
My bin file looks like this: https://ibb.co/bsj00jG
When I type 'which chd-project' in terminal (chd-project is one of the bash scripts which works), it says /usr/local/bin/chd-project - however I can't find a local file on my Mac.
When I type which id-project (the bash script that can't be found), it just says id-project not found.
If I set PATH=$HOME/bin, I can then call the id-project file. However, whenever I restart my terminal, it resets again. This can sometimes be buggy, though, as later commands in that same bash script can sometimes not be found.
When I type echo $PATH I get /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
However, like previously stated, I can call chd-project in my terminal (although it says usr/local/bin if i use 'which') but I can't call id-project.
Any help would be greatly appreciated.
Thanks

Your PATH variable is "reset" for every session you start. That's because the current session doesn't set environmental variables persistently. However, before a session is started it executes files that, for example, hold the value of PATH.
If you want to add this for every terminal you open, you should extend your path in your bash profile.
$ echo "export PATH=$PATH:$HOME/bin" >> $HOME/.bashrc
Please take not that you shouldn't overwrite your PATH variable, because it's used to find commands like mv, cp, etc.
EDIT:
I don't know Atom that well, but if you would open a regular (not an in-IDE) terminal it should work. It could be that Atom doesn't execute .bashrc for whatsoever reason. You could try to add it to to your profile.
$ echo "export PATH=$PATH:$HOME/bin" >> $HOME/.profile

Related

Reset export PATH back to its default value in Git Bash

I am new to bash/shell scripting and need to undo the effects created when I ran the command
export PATH=~/.local/bin:$PATH
and
export PATH=~/C:\Users\I860605\AppData\Roaming\Python\Python39\Scripts:$PATH
I was attempting to troubleshoot a Reuse tool and entered the command thinking it would fix the issue, however as a result none of my commands in Git Bash seem to work any longer, and normal commands for Python and other dependencies now result in command not found errors.
I also noticed that now my Git Bash CLI no longer picks up whether I'm cd'd into any of my repositories, with no display of (main) branch etc.
I found some other articles that were somewhat related to this, but they were for Ubuntu and Mac users, not for Git Bash on Windows, which is my scenario.
When I ran the command echo $Path, this is what was returned:
echo $Path
/c/Users/I860605/C:UsersI860605AppDataRoamingPythonPython39Scripts:/c/Users/I860605/.local/bin:/c/Users/I860605/.local/bin:C:UsersI860605AppDataRoamingPythonPython39Scripts
I saw that I could perhaps run source ~/.bash_profile or source ~/.bashrc and edit the file contents from there, but both commands return No such file or directory
How would I go about setting my Path back to how it was before this blunder?
If you don't want to open a new session, the simplest approach is:
to have a ~/.bashrc (with ~ or $HOME set by default to %USERPROFILE%)
set your PATH in it (like your export PATH=$PATH:~/.local/bin)
type "source ~/.bashrc": that will reset the PATH of the current session.
Type echo $PATH to then check the path is indeed at the expected value.

source /.bash_profile command not working

I am trying to refresh my aliases on my Mac (OS Catalina 10.15.6) after defining new aliases in my .bash_profile file with the command:
source ~/.bash_profile
But terminal keeps giving this error message:-bash: s: command not found
This is confusing because for the longest time this command worked. I even had it included in my .bash_profile file as an alias, where it worked fine.
I'm aware the problem could have to do it with an error in my PATH but I've never made any edits to my PATH so have no idea what the issue could be?
Thanks in advance.
My first instinct would be to check both ~/.bashrc, and /etc/bashrc if it exists. That is where I customarily define aliases, and it looks to me as though a bad alias may be your problem.
I'm not saying it was the one you made, although it might be. Just go through your rc and profile files and look for any aliases which might in any way clash with source.
I suspect the source command is working just fine and the problem is a bad line in the ~/.bash_profile itself that looks like it's trying to run a command named s. I would look in there for the problem.
It might help to run it with xtrace on via bash -x ~/.bash_profile – running it in a separate process like that won't have any of the presumably-desired side effects of sourceing it in your current shell, but you can see what it's trying to do so that you can fix it.
(You can also just set -x before the source and get both xtrace and running in the current shell; just be sure to set +x afterwards or your shell session will be full of debug output.)

How to run ~/.bash_profile in mac terminal

So I'm installing some things for coding and personal usage, and I need to run this in the terminal (I'm on Mac if you didn't read the title).
~/.bash_profile
It just says permission denied, Im running OSX 10.8.4 Mountain Lion. How do I bypass this?
On MacOS: add source ~/.bash_profile to the end of ~/.zshrc.
Then this profile will be in effect when you open zsh.
You would never want to run that, but you may want to source it.
. ~/.bash_profile
source ~/.bash_profile
both should work. But this is an odd request, because that file should be sourced automatically when you start bash, unless you're explicitly starting it non-interactively. From the man page:
When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands 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.
If you change .bash_profile, it only applies to new Terminal sessions.
To apply it to an existing session, run source ~/.bash_profile. You can run any Bash script this way - think of executing source as the same as typing commands in the Terminal window (from the specified script).
More info: How to reload .bash_profile from the command line?
Bonus: You can make environment variables available to OSX applications - not just the current Bash session but apps like Visual Studio Code or IntelliJ - using launchctl setenv GOPATH "${GOPATH:-}"
As #kojiro said, you don't want to "run" this file. Source it as he says. It should get "sourced" at startup. Sourcing just means running every line in the file, including the one you want to get run. If you want to make sure a folder is in a certain path environment variable (as it seems you want from one of your comments on another solution), execute
$ echo $PATH
At the command line. If you want to check that your ~/.bash_profile is being sourced, either at startup as it should be, or when you source it manually, enter the following line into your ~/.bash_profile file:
$ echo "Hello I'm running stuff in the ~/.bash_profile!"
No need to start, it would automatically executed while you startup your mac terminal / bash. Whenever you do a change, you may need to restart the terminal.
~ is the default path for .bash_profile
I was getting this error on zsh(mac os Big Sur 11.3), This is how i solved this :-
Go to Terminal.
cd /users/<yourusername>
Once you reach here issue a command :
ls -al
You will see a lot of files and one specific file .zprofile. This is your user profile. We need to edit this.
After this we need to edit the file. Issue the below command :
nano .zprofile
Once you issue this command file will be opened for edit. Add the path details for maven.
M2_PATH="/Users//code/apache-maven-3.8.1/bin" //add your path of maven diretory
PATH="${PATH}:${M2_PATH}"
export PATH
press ctrl + X and save the file.
Issue command after saving the file :
source .zprofile
Once done, you will be able to run the mvn command.
If the problem is that you are not seeing your changes to the file take effect, just open a new terminal window, and it will be "sourced". You will be able to use the proper PATH etc with each subsequent terminal window.

STTY may have been deleted?

I was installing ruby/rails/gem and homebrew and got frustrated. I think I deleted the stty folder. ANYWAY, what can I do? It doesn't seem to have ever happened on the internet before.
Running stty comes back with command not found...
The stty command can't be easily deleted; you'd have to be running with administrator privileges. It is supposed to be located in /bin/stty (try ls /bin/stty to make sure it's still there).
My guess is that you altered your shell's $PATH variable (which is a colon-separated list of directories that the shell searches for commands when you run them only by name).
Try this to see your current path: echo $PATH.
Then make sure that :/bin appears somewhere in that value, e.g.
export PATH=${PATH}:/bin
To fix this permanently, make sure that PATH is set in a shell startup file, e.g. .bashrc in your home directory.
I copied the stty from a friends mac. It seems to be working fine. I have run a few commands and there have been no issues yet.

In Mac OSX 10.5, it can't find my Terminal commands sudo, find, etc

I don't know what has happened, but in my Terminal on Mac OSX 10.5 it can no longer find my sudo command, or find command, etc. They are there because if I put /usr/bin/sudo or /usr/bin/find it works fine...
My .bash_login file looks like this:
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/bin$PATH"
My .bash_profile file looks like this:
export PATH="/Library/Frameworks/Python.framework/Versions/Current/bin:/Library/Python/2.5/site-packages/django_trunk/django/bin:/usr/local/mysql/bin:/usr/bin/sudo$PATH"
I'll say now, I don't really know what I'm doing with the Terminal. I'm just a beginner to it all, and I must of done something for the environment variables (is that what they're called?) to be lost. I presumed I'd just have to make sure the /usr/bin/ path is in my bash files, but they are, and it doesn't seem to work. Please help!
Also, when I do use the /usr/bin/find command, it says "Permission denied" to me, even though I am logged into Mac OSX as the System Administrator account. I don't understand.
Any help would be grand. Thank you - James
It looks like both of your PATH exports are malformed:
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/bin$PATH"
The end bit there won't work. It should be:
export PATH=/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/bin:$PATH
Notice the colon before '$PATH'? It's important ;)
Also, the double quotes are not necessary.
If this doesn't work, we will need more information. It is possible that something else is modifying your path even after your shell configurations are loaded.
Can you post the results of:
$ echo $PATH
Configuration files are not always a good indication of the current environment variables, since they are modified by many programs and files, all across your system. To see all of your environment variables, you can run:
$ env
This should fix the problem completely and permanently.
first, export environment paths by using below command in the terminal.
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/b‌​in
now you have the commands you want. (eg. try ls. You'll see the command is working). But this is only for the current session. If you close the terminal and open a new one, you will have the previous issue. To make this change permanent, use below command,
go to home directory
cd ~
open .bash_profile file in nano / vim (I'm using nano here)
nano .bash_profile
This will open up nano editor. In a new line, paste the following;
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:$PATH
press 'control'+'o' to save (WriteOut) and 'control'+'x' to exit nano.
All done ! Now try the commands.
Check out --- http://www.sweeting.org/mark/blog/2008/05/26/mac-os-x-tip-setting-path-environment-variables
I went trough the same issue and here is how I solved it.
First of all I reverted the file to its original doing this way
/usr/bin/nano ~/.bash_profile
In my case I was not able to make work any command alias. Even vi or vim didnt work without specifying the full path of that command.
If nano is not installed just replace nano in the command by the editor installed
After that just restart the computer. In my case as I said bellow I could not use any command. When trying to do /usr/bin/source ~/.bash_profile
that command failed. So I had to restart the OS and it worked

Resources