.bash_profile won't open - bash

When I run open .bash_profile a new terminal opens and
Last login: Wed Nov 30 19:01:22 on ttys004
/Users/MyName/.bash_profile ; exit;
➜ ~ /Users/MyName/.bash_profile ; exit;
[Process completed]
My permissions are
-rwxr-xr-x# 1 MyName staff 639 Nov 30 19:02 .bash_profile
Also, when I run ls -la, .bash_profile is colored red. I don't know why. It wasn't like that before.
All I was trying to do was create an alias, but now I can't even open my .bash_profile.

tl;dr
To ensure that you open a file in your default text editor using the macOS open CLI, use:
open -t ~/.bash_profile
Otherwise, if the file needn't be excecutable, run chmod -x <file> (chmod -x ~/.bash_profile, in this case) to make open behave like it did before.
From you question I infer that you're on macOS (OS X).
What the macOS open CLI does when passed a file depends on the file's suffix (extension), and, in the absence of one, on whether the file has the executable (r) permissions bit(s) set (if not, the file opens in the standard text editor, which is what you saw before).
A file displaying in red when you use ls -a (the -a being necessary to show hidden items such as .bash_profile), implies that the file is indeed executable by you.
A suffix-less executable (text) file causes open to run it in a new terminal window as a shell script, which is what you're seeing.
In other words: at some point, unbeknownst to you, executable permissions were assigned to ~/.bash_profile, which explains the change in behavior.
However, there is no need for ~/.bash_profile to be executable, because it is sourced by Bash on startup.
As stated above, either remove the executable permissions, or simply use open -t to open it.

Related

How to set .bash_profile, if it does not exist yet. I want to launch sublime from a command line in Mac

I want to launch sublime from a command line in Mac, using subl filename. It seems to involve dealing with .bash_profile. But I didn't locate the file. What steps to be taken?
A typical install of OS X 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.
Type . .bash_profile to reload .bash_profile and update any functions you add. Notice the space between the two dots!
Update
I'm on Mac OS Mojave.
Open Terminal.app and paste bellow line,
(1) touch .bash_profile
(2) open -a TextEdit.app .bash_profile
this will open a blank page in TextEdit.app , from here you can add,update,delete code.
I hope this will help someone.
just create a new file - it doesn't come default with your computer. all under your user directory - ex. /Users/username
touch .bash_profile
~/.bash_profile
Q1. How to check if .bash_profile exists or not in my mac?
Solution: If you're using macOS 10.14 (Mojave) or below. Then open the Terminal.app. Run the following command to check if the .bash_profile exists or not in your mac.
if [ -r ~/.bash_profile ];
then
echo "Yes, file exists"
else
echo "No, file does not exists"
fi
After running the above command if you get the following line - "Yes, file exists" printed in your Terminal.app. That means the file exists in your mac.
If you get the following line - "No, file does not exist" printed in your Terminal.app. That means the file does not exist in your mac.
To create a .bash_profile file in your mac. Run the following command,
touch ~/.bash_profile
To restrict access to the .bash_profile. Run the following command,
chmod 700 ~/.bash_profile
Q2. I want to launch sublime from a command line in Mac?
Solution: To launch sublime from mac. You can make a symlink to subl. Assuming you've placed Sublime Text in the Applications folder, and that you have a ~/bin directory in your path, you can run the following command:
ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" ~/bin/subl
For more details visit the official sublime documentation

/Users/DylanRichards/.profile:source:2: no such file or directory: QSTK/local.sh

I'm going to open this up again. I installed this thing called QSTK for some financial calculations. Now every time I open my terminal, I get this error:
/Users/DylanRichards/.profile:source:2: no such file or directory: QSTK/local.sh
Because of this error, I am unable to run programs in Sublime Text as well.
How the heck do I get rid of this?
Latest
After trying KEYSER's suggestion, here are the contents of the .profile file:
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
source QSTK/local.sh
PATH=$PATH:/Applications/Darwine/Wine.bundle/Contents/bin/
QSTK seems to have injected some line into your profile file, which is loaded by the terminal at startup. That line is trying to run a script which doesn't exist.
To get rid of the error remove line 2 in .profile (and possibly surrounding, related, lines).
It's a hidden file, so you can e.g. open it from a terminal. for example:
open -a "/Applications/Sublime Text.app/" ~/.profile
if that's the name of your sublime app (tab to autocomplete)
How do I find and edit ~/.profile?
In Terminal:
$ cd ~
$ touch .profile
$ open -e .profile
This will open the .profile file in TextEdit for your editing enjoyment.
Once you have completed your editing, save the file in TextEdit and then, in Terminal:
$ source .profile
This should avoid the need for a system restart but always a good idea to do a restart anyway when playing with fundamentals like .____ files.

Open terminal and it reads a line to me?

I know the settings were changed so that everytime I open my terminal it reads my name to me via the say command. Where is the file that it was saved in (so I can delete that line)?
It sounds like the say command has been added to one of your shell’s startup files. Try this command to search for the say command in all of them:
grep say ~/.bashrc ~/.bash_profile ~/.bash_login /etc/bashrc /etc/profile
If that doesn’t turn anything up, you can run sudo filebyproc.d to start a trace of all files being access on the filesystem, open a new terminal, and look for which files are read by the bash command. The filebyproc.d(1m) manpage has more info if you’re forced to go down this route.

.bashrc: Permission denied

I try to work with a project in vagrant.
I have made the command vagrant ssh, and connected to VM. Now I need to edit .bashrc file to set path to the source code. But first I couldn't find that file. So I googled and find that the way is call command ~/.bashrc. But doing this I get message, that I have no access to it:
[vagrant#nupic-vagrant:~]$ ~/.bashrc
-bash: /home/vagrant/.bashrc: Permission denied
So what to do now?
UPD.
I can't find the .bashrc file.
When I try to make command ls -a I get following:
[vagrant#nupic-vagrant:~]$ ls -a
. .bash_logout cleanup.sh sshd.sh .veewee_params
.. .bash_profile minimize.sh vagrant.sh .veewee_version
.bash_history .bashrc .ssh .vbox_version .zsh_profile
[vagrant#nupic-vagrant:~]$ locate .bashrc
/etc/skel/.bashrc
/home/vagrant/.bashrc
/var/chef/backup/etc/skel/.bashrc.chef-20130614181911
/var/chef/backup/home/vagrant/.bashrc.chef-20130614181912
[vagrant#nupic-vagrant:~]$
But only the place where I can find some of those files is the directory where cygwin is installed. Pls, see illustrations, they reflect relations between directories vagrant and cygwin.
.bashrc is not meant to be executed but sourced. Try this instead:
. ~/.bashrc
or, equivalently
source ~/.bashrc
See the reference about the . (aka source) builtin.
Note that if what you're looking for is to restart your Bash session after modifying your ~/.bashrc file, you might as well use:
exec bash
That will replace your current Bash session (thanks to exec) by a new session.
If you want to edit that file (or any file in generally), you can't edit it simply writing its name in terminal. You must to use a command to a text editor to do this. For example:
nano ~/.bashrc
or
gedit ~/.bashrc
And in general, for any type of file:
xdg-open ~/.bashrc
Writing only ~/.bashrc in terminal, this will try to execute that file, but .bashrc file is not meant to be an executable file. If you want to execute the code inside of it, you can source it like follow:
source ~/.bashrc
or simple:
. ~/.bashrc
If you can't access the file and your os is any linux distro or mac os x then either of these commands should work:
sudo nano .bashrc
chmod 777 .bashrc
it is worthless
The .bashrc file is in your user home directory (~/.bashrc or ~vagrant/.bashrc both resolve to the same path), inside the VM's filesystem. This file is invisible on the host machine, so you can't use any Windows editors to edit it directly.
You have two simple choices:
Learn how to use a console-based text editor. My favourite is vi (or vim), which takes 15 minutes to learn the basics and is much quicker for simple edits than anything else.
vi .bashrc
Copy .bashrc out to /vagrant (which is a shared directory) and edit it using your Windows editors. Make sure not to save it back with any extensions.
cp .bashrc /vagrant
... edit using your host machine ...
cp /vagrant/.bashrc .
I'd recommend getting to know the command-line based editors. Once you're working inside the VM, it's best to stay there as otherwise you might just get confused.
You (the vagrant user) are the owner of your home .bashrc so you do have permissions to edit it.
Once edited, you can execute it by typing
source .bashrc
I prefer to logout and in again (there may be more than one file executed on login).
Please find the step to fix bash restricted error on Linux servers.
If you are getting below restricted message while try to login to the server by using your credentials , then it might be an issue with lack of directory permissions in the server.
Because of this permission issue we were unable to navigate to required directories and getting error “bash: cd: restricted”
Fix : To release bash restriction error use the highlighted command in Linux server -bash -f
As same if wants to restrict the permission use the highlighted command - bash -r
Once you executed the bash -f command the restrictions will be released from the directories and we can able to proceed with patch scripts.

when i type commands in the terminal i get an error - "command not found"

gal-harths-iMac:~ galharth$ ruby -v
-bash: ruby: command not found
gal-harths-iMac:~ galharth$ open -e .bash_profile
-bash: open: command not found
what shoud i do?
my .bash_profile and .profile and .bashrc are empty, i need to write something in them?..
I suspect that you have something overriding your default path (like .bash_profile or .bashrc) open is a valid command on os x, for me man open returns
NAME
open -- open files and directories
SYNOPSIS
open [-e] [-t] [-f] [-W] [-R] [-n] [-g] [-h] [-b bundle_identifier]
[-a application] file ... [--args arg1 ...]
DESCRIPTION
The open command opens a file (or a directory or URL), just as if you had
double-clicked the file's icon. If no application name is specified, the
default application as determined via LaunchServices is used to open the
specified files.....
Likewise it is possible ruby is installed but not on the path. My best guess would be to delete or rename your .bashrc and .bash_profile files and log off as that user and log back in, to reset your bash session.
To test if it is a user level issue, create a new account under system preferences -> accounts and then log in as that user, open a terminal and type ruby -v or man open, etc to see if you can do it on a default user account. If that works, it is most certainly some bash settings you have customized in your 'gal-harth' account.
The first step to figuring out what's gone wrong is to see what your current PATH is.
echo $PATH
If that returns a blank line, something has gone horribly wrong. You can fix it temporarily by running:
export PATH=/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
And that should get you ruby, as well as open, and man, and a bunch of other commands.
The thing is, the PATH says “Look in these directories for binary files” and not “Here is an explicit list of binary files I want to use.”
Ruby is not installed. You need to install it from their website here.
Similarly, "open" is not any bash command I've ever heard of.
Upon further reflection, are you trying to install rvm?
If you have already installed Ruby, is its location already set in your PATH variable? If not, you can add it to your .bash_profile like this:
echo 'export PATH=/wherever/ruby/is/located:$PATH' >> ~/.bash_profile
Something is screwing up your PATH -- you shouldn't need to have a .profile (or any of its variants) to have the default PATH set properly. Generally, when the PATH gets screwed up, it's because it's being set wrong in one of the various profile files. When you say .bash_profile and .profile and .bashrc are empty, do you mean they exist but don't have anything in them, or they don't exist at all? Also, do you have a .bash_login or .bashrc file?
I'd take a closer look around your home directory. Use /bin/ls -ld ~/.* to list all of the invisible files in your home directory, and look for anything with "sh", "profile", or "rc" in the name. Also, run /usr/bin/grep PATH ~/.* to see if any of the invisible files mention PATH -- if any do, they're likely suspects.
just type bash_filename or ./_filename

Resources