Terminal cannot find bashrc file when using source command - bash

I recently downloaded mysql. What am I doing wrong when I source the bash file?
The path for my bashrc file is:
Users/Name/bashrc
(I moved it from a different location, can't remember from where, but I figure that if the path is right, it shouldn't matter)
In the terminal, I'm supposed to use:
source ~/.bashrc
When I type that and press enter, it returns:
-bash: /Users/Name/.bashrc: No such file or directory
The bash file itself says:
source /etc/bash.bashrc
source ~/.bashrc
export PATH=$PATH:/usr/local/mysql/bin

You need to have the file .bashrc located in ~ to be able to source it. ~ expands to $HOME (/Users/Name in your case).
If the output of cd ~ && ls -la doesn't contain .bashrc, then you don't have that file, so you cannot source it.
Based on your comments, your file is ~/bashrc.sh (not a great name). If you delete the first two lines of this file, it will work properly. Note that you'll have to source this file every time you open a new terminal. You can add the line
export PATH=$PATH:/usr/local/mysql/bin
to a file that gets automatically sourced upon login, like ~/.bash_profile, /etc/bash.bashrc (not recommended), or create a ~/.bashrc and include that line.

Related

How to find where my PATH variable is being generated and how to edit it in Mac OSX Terminal?

Currently I am in Mac OSX and when I try to find what my PATH is via Terminal, I get:
> echo $PATH
/Users/User1/google-cloud-sdk/bin:/usr/local
/bin:/usr/local/sbin:/usr/local/bin:
/usr/bin:/bin:/usr/sbin:/sbin:
/Library/TeX/texbin:/opt/X11/bin:
/Applications/Wireshark.app/Contents/MacOS
I would like to remove google-cloud-sdk, wireshark, and tex from PATH, but have no idea how to do it. When I look inside my etc/profile file, none of these apps are listed. Is there a way to clean up by $PATH? Thanks.
I just found the Wireshark path file in :
/etc/paths.d/
Go to your home directory. If you open a fresh Terminal window/session, this will probably have you in your home directory.
Type,
ls -al
This should give you a list of the files in your home directory, including invisible files. Check there is a file called .bash_profile. The "." means it is an invisible file.
If that exists (as it should) type:
nano .bash_profile
This will open this file in the text editor program called "nano". In this file you should find statements like:
export PATH="/Library/Frameworks ....
There should be similar PATH statement for the options you want to remove.
Delete those lines and then exit nano by ^O for writing out, and you need to print Y to save. Then ^X to exit nano.

How to fix -bash: No such file or directory

Whenever I open up a new terminal I get the following:
-bash: /Users/chrisjayden/.profile: No such file or directory
I tried searching Q&A's but couldn't find anything relevant.
+ I'm pretty new to this stuff
-bash indicates that your terminal is starting a login shell. That means bash will try to execute the contents of the first of the following three files that it finds.
~/.bash_profile
~/.bash_login
~/.profile
If all three were missing, bash would not complain. Therefore, we can assume that it finds one of the first two files, and in that file is a line like source ~/.profile or . ~/.profile, which will produce the error shown if ~/.profile does not exist.
The easiest solution is to simply create an empty .profile file in your home directory.

How does one locate a .zshrc file?

I used Homebrew to install Z shell (zsh) 5.0.7 on my Mac.
For the life of me, .zshrc is nowhere to be found. It is not in ~. Is is not in /etc or /etc/zshrc as they suggest here: http://zshwiki.org/home/config/files
Am I supposed to create it myself?
Sure. If it's not there already, create it yourself.
$ touch ~/.zshrc
You can run the helper script zsh-newuser-install from the prompt, and it will walk you through the process to create an initial .zshrc in your home directory.
As kyranjamie mentioned, you can create it using following command
$ touch ~/.zshrc
Example content of .zshrc file:
PATH=$PATH:/your_path_goes_here
In order to find any file on a Unix-based system, you can try the command:
$ locate filename
It should list all the paths where the corresponding file exists.
In Unix based systems, touch command followed by name will create an empty file in the present directory.
the modification and access time of each file is also updated with the use of touch command.
In your case, to create .zshrc file, you can use the touch command as :
$ touch ~/.zshrc

How to remove entry from $PATH on mac

I was trying to install Sencha Touch SDK tools 2.0.0 but could not run it properly. It created an entry in the $PATH variable.
Later I deleted the sencha sdk tools folder but didn't realize that the path variable is still there.
When i did echo $PATH I got -
/Applications/SenchaSDKTools-2.0.0-beta3:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
I searched on how to remove variables from $PATH and followed these steps :
Gave the command PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin"
Did echo $PATH which showed /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
gave the command export PATH
Closed terminal and reopened it. Gave the command echo $PATH. This time I got
/Applications/SenchaSDKTools-2.0.0-beta3:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
Can anyone tell me what am i doing wrong?
echo $PATH and copy it's value
export PATH=""
export PATH="/path/you/want/to/keep"
Check the following files:
/etc/bashrc
/etc/profile
~/.bashrc
~/.bash_profile
~/.profile
~/.MacOSX/environment.plist
Some of these files may not exist, but they're the most likely ones to contain $PATH definitions.
On MAC OS X Leopard and higher
cd /etc/paths.d
There may be a text file in the above directory that contains the path you are trying to remove.
vim textfile //check and see what is in it when you are done looking type :q
//:q just quits, no saves
If its the one you want to remove do this
rm textfile //remove it, delete it
Here is a link to a site that has more info on it, even though it illustrates 'adding' the path. However, you may gain some insight.
What you're doing is valid for the current session (limited to the terminal that you're working in). You need to persist those changes. Consider adding commands in steps 1-3 above to your ${HOME}/.bashrc.
If you're removing the path for Python 3 specifically, I found it in ~/.zprofile and ~/.zshrc.
$PATH contains data that is referenced from actual files. Ergo, you should find the file containing the reference you want to delete, and then delete said reference.
Here is a good list to run through progressively [copied from #Ansgar's answer with minor updates].
/etc/bashrc
/etc/profile
~/.bashrc
~/.bash_profile
~/.profile
~/.MacOSX/environment.plist
/etc/paths
/etc/paths.d/
Note that /etc/paths.d/ is a directory that contains files with path references. For example, inside this directory may be a file called, say, fancy-app, and inside this file you'll see an entry like below:
/path/to/fancy-app
This path will appear in your $PATH and you can delete the entry in the file to remove it, or you can delete the file if it has only the one reference you want to remove.
Use sudo pico /etc/paths inside the terminal window and change the entries to the one you want to remove, then open a new terminal session.
when you login, or start a bash shell, environment variables are loaded/configured according to .bashrc, or .bash_profile. Whatever export you are doing, it's valid only for current session. so export PATH=/Applications/SenchaSDKTools-2.0.0-beta3:$PATH this command is getting executed each time you are opening a shell, you can override it, but again that's for the current session only. edit the .bashrc file to suite your need. If it's saying permission denied, perhaps the file is write-protected, a link to some other file (many organisations keep a master .bashrc file and gives each user a link of it to their home dir, you can copy the file instead of link and the start adding content to it)
Close the terminal(End the current session). Open it again.
If the manual export $PATH method does not seem to be working after you close the terminal and open again, definitely check the shell configuration files.
I found a small script that kept adding some more path in front of the $PATH everytime it was open.
For zsh you can check the ~/.zshrc file.

How to make a shell script global?

I am on Mac's OS 10.6, and I am trying to learn a thing or two about shell scripting. I understand how to save a shell script and make it executable, but I am wondering what I can do or where I can save the file to make it global (that is, accessible no matter what folder I am in).
For example, if I save a .sh file in the /Users/username/ directory and make it executable, I can only execute that script in that specific directory. If I navigate to /Users/username/Downloads, for example, I can't execute the script.
Also, any suggestions of resources for learning more about shell scripting would be helpful. Thanks
/usr/local/bin would be the most appropriate location. Mac OS X has it in the PATH by default
There are two ways to do it -
Put your script in usr/local/bin and make sure it is executable(chmod +x my_script)(This is already set in the path, you can check by doing an echo $PATH)
Create a folder in your home directory called bin. (For your personal scripts)
cd ~ (Takes you to your home directory)
mkdir bin (create a bin folder)
vim .bash_profile (to set path environment variable)
export PATH=~/bin:$PATH (Press i then add this line and then do esc and type :wq)
Now you can just type the name of your script and run it from anywhere you want.
** NOTE: If you want to run the script with a shortened command rather than typing your entire filename, add the following to your .bash_profile:
alias myscript='my_script.sh'
Then you can run the script by simply typing myscript. (you can sub in whatever alias you'd like)
Traditionally, such scripts either go in ~/bin (ie: the bin directory in your home directory) or /usr/local/bin/ The former means the script will only work for you, the latter is for scripts you want anybody on the system to be able to run.
If you put it in ~/bin, you may need to add that to your PATH environment variable. /usr/local/bin should already be on the path.
In mac operating system
Open bash ~/.bashrc file.
add path of your script in your bashrc file , using
export PATH="$PATH:/Users/sher.mohammad/Office/practice/practiceShell"
Open your ~./bash_profile file and add [[ -s ~/.bashrc ]] && source ~/.bashrc
open new terminal window
Now whenever you will open your terminal your script will be loaded
This one is super easy if you are familiar with your bashrc file! This will entirely use just your .bashrc file and takes 2 seconds to accomplish.
(I use Arch Linux Manjaro so I use .bashrc located in my home directory)
The code to be placed in your .bashrc file:
# Simple bashrc method to launch anything in terminal from any directory
YOURCOMMAND () {
cd /path/to/directory/containing/your/script/ && ./YOURSCRIPT
}
As you can see, first you use the simple 'cd' command and give it the directory of the scripts location, then use '&&' so that you can make the next command executed right after, and finally open your script just as you would normally! Super easy and saved right in your .bash file! :)
Hope I've helped someone!
Sincerely,
AnonymousX
On using bash shell, write that script as function and then put it to the .bashrc or source the file which containing that function by "source file_name"
Now execute the script by function call in the shell.
Either saving it in /usr/bin (or any other directory present in PATH) or editing PATH to include the directory you saved it in will basically make it run in any directory.
from the working directory of 'script.sh'" mv [script.sh] /usr/local/bin"( not tested but seems to be the least complex way IMO.)
You should put it in the global executable directory on your machine. I think that would usually be /usr/bin on Unix-based operating systems (this would however most often require super user privileges on that machine).
You could also put it in any other directory that is in the $PATH environment variable, although it would only work for those users who have that directory in that variable.
You can find the value of $PATH by typing echo $PATH in a shell. The directories are separated by :.

Resources