.exrc not working - bash

i am new to unix. I have created a .exrc file in my home folder. I just entered set nu and set ai in the .exrc to check. But whenever I'm opening vi editor, this doesn't work.
Further, I entered as superuser, by typing su and password, and now opened a new file in vi editor, now it is working.
Do we need to have superuser privilege to have this? Please help me on this.

Here's what I think is happening:
vi is actually a link or alias to vim, a modern version of vi.
You have a .vimrc file, which vim will read if present instead of .exrc.
As superuser, vi is not a link to vim, and ignores .vimrc and reads .exrc.
Check the output of
$ which vi
$ ls -l `which vi`
$ ls ~/.vimrc
The solution would be to use .vimrc instead of .exrc.

First, ensure that your ~/.exrc is readable and that it actually works by running :source $HOME/.exrc in vi.
If you are using a more "traditional" vi (not Vim), it may be due to addtional permission issues. The POSIX document about ex says:
Historically, the 4 BSD ex and vi read the $HOME and local .exrc files if they were owned by the real ID of the user, or the sourceany option was set, regardless of other considerations. This was a security problem because it is possible to put normal UNIX system commands inside a .exrc file. POSIX.1-2017 does not specify the sourceany option, and historical implementations are encouraged to delete it.
Make sure you are the owner of ~/.exrc: chown <your-username> ~/.exrc
In addition, if you happen to be using ex-vi ("Traditional vi"), ~/.exrc must not be group- or world-writable. Run chmod go-w ~/.exrc to ensure this. Reference from the changelog of the 09/23/00 release:
POSIX.2: If .exrc files are world or group writeable, they are ignored
unless "sourceany" is set.

Related

-bash: emacs.profile: command not found

I am trying to resolve this issue and trying to get it work. What are all the steps of the solution? I really don't know what to do on my Mac terminal
-bash: emacs.profile: command not found
What are you trying to accomplish? If you're trying to open a file named .profile with emacs you'll want to add a space between the command emacs and the file .profile:
$ emacs .profile
If you are typing emacs.profile you will get an error because you are mixing together the command emacs and the file you are trying to edit: .profile. Therefore the solution would be to add a space between them like someone suggested: $ emacs .profile.
On the other hand, if you are typing emacs .profile and you are still getting: -bash: emacs: command not found, this means that you haven't installed emacs yet and your system doesn't recognize that instruction.
Emacs is a very popular text editor and is widely used by technical users, but it is not installed by default.
Solution 1:
If you want to edit a file (in this case ~/.profile) you don't need emacs to do so, you could just use a normal text editor or a pre-installed text editor using your terminal like:
$ vi ~/.profile or $ nano ~/.profile
(Guide to use Vi/Vim text editors) (Guide to use Nano).
Solution 2:
If you want to install emacs to edit that file, you can do so using brew:
$ brew update
$ brew install emacs
And after that you can use:
$ emacs ~/.profile
You can find other ways to install emacs here.
Important Note:
One important thing to consider in this example, is that if you are trying to edit the .profile file (i.e. to set an exported environment variable) you should know that the name of the .profile file might vary from one Mac system to another. The name of this profile configuration file depends on the shell of your system, if you are using bash the name of this profile will be .bashrc or .bash_profile.
Before editing your profile file make sure what is the name of the profile file you are trying to edit. To do so, you can use:
$ cd $HOME
to go to the Home folder and then use:
$ ls -al
to see hidden files (These files starting with . are hidden files). You will find the profile file of your system there.

How to update PATH to find nvcc for CUDA 8.0? [duplicate]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about a specific programming problem, a software algorithm, or software tools primarily used by programmers. If you believe the question would be on-topic on another Stack Exchange site, you can leave a comment to explain where the question may be able to be answered.
Closed 2 years ago.
Improve this question
On Linux, how can I add a directory to the $PATH so it remains persistent across different sessions?
Background
I'm trying to add a directory to my path so it will always be in my Linux path. I've tried:
export PATH=$PATH:/path/to/dir
This works, however each time I exit the terminal and start a new terminal instance, this path is lost, and I need to run the export command again.
How can I do it so this will be set permanently?
You need to add it to your ~/.profile or ~/.bashrc file.
export PATH="$PATH:/path/to/dir"
Depending on what you're doing, you also may want to symlink to binaries:
cd /usr/bin
sudo ln -s /path/to/binary binary-name
Note that this will not automatically update your path for the remainder of the session. To do this, you should run:
source ~/.profile
or
source ~/.bashrc
There are multiple ways to do it. The actual solution depends on the purpose.
The variable values are usually stored in either a list of assignments or a shell script that is run at the start of the system or user session. In case of the shell script you must use a specific shell syntax and export or set commands.
System wide
/etc/environment List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME. Used by PAM and systemd.
/etc/environment.d/*.conf List of unique assignments. Allows references. Perfect for adding system-wide directories like /usr/local/something/bin to PATH variable or defining JAVA_HOME. The configuration can be split into multiple files, usually one per each tool (Java, Go, and Node.js). Used by systemd that by design do not pass those values to user login shells.
/etc/xprofile Shell script executed while starting X Window System session. This is run for every user that logs into X Window System. It is a good choice for PATH entries that are valid for every user like /usr/local/something/bin. The file is included by other script so use POSIX shell syntax not the syntax of your user shell.
/etc/profile and /etc/profile.d/* Shell script. This is a good choice for shell-only systems. Those files are read only by shells in login mode.
/etc/<shell>.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used in non-login mode.
User session
~/.pam_environment. List of unique assignments, no references allowed. Loaded by PAM at the start of every user session irrelevant if it is an X Window System session or shell. You cannot reference other variables including HOME or PATH so it has limited use. Used by PAM.
~/.xprofile Shell script. This is executed when the user logs into X Window System system. The variables defined here are visible to every X application. Perfect choice for extending PATH with values such as ~/bin or ~/go/bin or defining user specific GOPATH or NPM_HOME. The file is included by other script so use POSIX shell syntax not the syntax of your user shell. Your graphical text editor or IDE started by shortcut will see those values.
~/.profile, ~/.<shell>_profile, ~/.<shell>_login Shell script. It will be visible only for programs started from terminal or terminal emulator. It is a good choice for shell-only systems. Used by shells in login mode.
~/.<shell>rc. Shell script. This is a poor choice because it is single shell specific. Used by shells in non-login mode.
Notes
GNOME on Wayland starts a user login shell to get the environment. It effectively uses the login shell configurations ~/.profile, ~/.<shell>_profile, ~/.<shell>_login files.
Man pages
environment
environment.d https://linux.die.net/man/1/environment.d
bash
dash
Distribution-specific documentation
Ubuntu
Arch Linux
Related
Difference between Login Shell and Non-Login Shell?
In Ubuntu, edit /etc/environment. Its sole purpose is to store environment variables. Originally the $PATH variable is defined here.
This is a paste from my /etc/environment file:
PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games"
So you can just open up this file as root and add whatever you want.
For immediate results,
Run (try as normal user and root):
source /etc/environment && export PATH
If you use Z shell (zsh), add this line right after the comments in /etc/zsh/zshenv file:
source /etc/environment
I encountered this little quirk on Ubuntu 15.10 (Wily Werewolf), but if your zsh is not getting the correct PATH, this could be why.
For Bash, you can put the export declaration in ~/.bashrc. For example, my .bashrc contains this line:
export PATH=/var/lib/gems/1.8/bin:/home/ash/.bin:$PATH
You may set $PATH permanently in two ways.
To set the path for a particular user:
You may need to make the entry in file .bash_profile in the home directory for the user.
E.g, in my case I will set the java path in the Tomcat user profile*
echo "export PATH=$PATH:/path/to/dir" >> /home/tomcat/.bash_profile
To set a common path for all system users, you may need to set the path like this:
echo "export PATH=$PATH:/path/to/dir" >> /etc/profile
You can use on CentOS or Red Hat Linux (RHEL) for the local user:
echo $"export PATH=\$PATH:$(pwd)" >> ~/.bash_profile
This adds the current directory (or you can use another directory) to the PATH. This makes it permanent, but it takes effect at the next user logon.
If you don't want do a re-logon, then you can use:
source ~/.bash_profile
That reloads the # User specific environment and startup programs. This comment is present in file .bash_profile.
You can also set it permanently, editing one of these files:
/etc/profile (for all users)
~/.bash_profile (for current user)
~/.bash_login (for current user)
~/.profile (for current user)
You can also use /etc/environment to set a permanent PATH environment variable, but it does not support variable expansion.
Extracted from: Linux: Añadir ruta al PATH
I think the most elegant way is:
Add this in the ~/.bashrc file.
Run this command:
gedit ~/.bashrc
Add your path inside it:
export PATH=$PATH:/opt/node/bin
source ~/.bashrc
(Ubuntu)
Modify the "/etc/profile" file:
vi /etc/profile
Press the I key to enter editing mode and move the cursor to the end of the file. Additional entries:
export PATH=$PATH:/path/to/dir;
Press the Esc key to exit edit mode, and :wq to save the file.
Make the configuration effective
source /etc/profile
Explanation:
The profile file works for all users. If you want it to be valid only for the active user, change the ".bashrc" file.
I stumbled across this question yesterday when searching for a way to add a folder containing my own scripts to the PATH - and was surprised to find out that my own ~/.profile file (on Linux Mint 18.1) already contained this:
# set PATH so it includes user's private bin if it exists
if [ -d "$HOME/bin" ] ; then
PATH="$HOME/bin:$PATH"
fi
Thus, all I had to do was create the folder ~/bin and put my scripts there.
You can add that line to your console configuration files (e.g., .bashrc, or to .profile).
After so much research, I found a simple solution for this (I am using Elementary OS), inspired by Flutter – Step by Step Installation on Linux – Ubuntu.
Run the following command to open the .bashrc file in edit mode. (You
may also use vi or any other editor).
~$ sudo nano ~/.bashrc
Add the following line at the end of the file and save.
export PATH="[FLUTTER_SDK_PATH]/flutter/bin:$PATH"
For example:
export PATH="/home/rageshl/dev/flutter/bin:$PATH"
I believe this is the permanent solution for setting the path in Flutter in a Ubuntu distribution.
It can be directly added by using the following command:
echo 'export PATH=$PATH:/new/directory' >> ~/.zshrc
source ~/.zshrc
One way to add a permanent path, which worked for me, is:
cd /etc/profile.d
touch custom.sh
vi custom.sh
export PATH=$PATH:/path according to your setting/
Restart your computer and here we go; the path will be there permanently.
Add script file [name_of_script].sh to the /etc/profile.d folder with the line:
export PATH=$PATH:/dir
Every script within the /etc/profile.d folder is automatically executed by /etc/profile on login.
My answer is in reference to the setting up of a Go environment on Ubuntu Linux (amd64). I have faced the same trouble of setting the path of environment variables (GOPATH and GOBIN), losing it on terminal exit and rebuilding it using the source <file_name> every time.
The mistake was to put the path (GOPATH and GOBIN) in ~/.bash_profile file. After wasting a few good hours, I found that the solution was to put GOPATH and GOBIN in the ~/.bash_rc file in the manner:
export GOPATH=$HOME/go
export GOBIN=$GOPATH/bin
export PATH=$PATH:$GOPATH:$GOBIN
And in doing so, the Go installation worked fine and there were no path losses.
The reason with which this issue can be related is that settings for non-login shells, like your Ubuntu terminal or GNOME terminal where we run the Go code, are taken from the ~./bash_rc file and the settings for login shells are taken from ~/.bash_profile file. And from the ~/.profile file if the ~/.bash_profile file is unreachable.
The files where you add the export command depends on if you are in login-mode or non-login-mode.
If you are in login-mode, the files you are looking for are either /etc/bash or /etc/bash.bashrc.
If you are in non-login-mode, you are looking for the file /.profile or for the files within the directory /.profiles.d
The files mentioned above is where the system variables are.
Permanently add to the PATH variable
Global:
echo "export PATH=$PATH:/new/path/variable" >> /etc/profile
Local (for the current user only):
echo "export PATH=$PATH:/new/path/variable" >> ~/.profile
For global, restart. For local, relogin.
Example
Before:
$ cat /etc/profile
#!/bin/sh
export PATH=/usr/bin:/usr/sbin:/bin:/sbin
After:
$ cat /etc/profile
#!/bin/sh
export PATH=/usr/bin:/usr/sbin:/bin:/sbin
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/new/path/variable
Alternatively you can just edit file "profile":
$ cat /etc/profile
#!/bin/sh
export PATH=/usr/bin:/usr/sbin:/bin:/sbin:/new/path/variable
Another way (thanks gniourf_gniourf):
echo 'PATH=$PATH:/new/path/variable' >> /etc/profile
You shouldn't use double quotes here! echo 'export
PATH=$PATH:/new/path/variable'... And by the way, the export keyword
is very likely useless as the PATH variable is very likely already
marked as exported. – gniourf_gniourf
Zues77 has the right idea. The OP didn't say "How can I hack my way through this?". The OP wanted to know how to permanently append to $PATH:
sudo nano /etc/profile
This is where it is set for everything and is the best place to change it for all things needing $PATH.
Let's say you're running macOS. You have a binary you trust and would like to make available across your system, but don't necessarily want the directory in which the binary is to be added to your PATH.
You can opt to copy/move the binary to /usr/local/bin, which should already be in your PATH. This will make the binary executable like any other binary you may already have access to in your terminal.
The simplest way is the following line,
PATH="<directory you want to include>:$PATH"
in your .bashrc file in the home directory.
It will not get reset even if you close the terminal or reboot your PC. It's permanent.
This is a one-liner. It adds a line to the .bashrc. That line is going to check if the directory has already been added to the path and append if not. This will prevent duplicating your directory in the path every time you source .bashrc.
echo "[[ \":\$PATH:\" != *\":$(pwd)/path/to/add:\"* ]] && export PATH=\"\${PATH:+\${PATH}}:$(pwd)/path/to/add\"" >> ~/.bashrc
source ~/.bashrc
I think the most elegant way is:
Add this in the ~./bashrc file:
if [ -d "new-path" ]; then
PATH=$PATH:new-path
fi
source *~/.bashrc*
(Ubuntu)
For a Debian distribution, you have to:
edit file ~/.bashrc. E.g: vim ~/.bashrc
add export PATH=$PATH:/path/to/dir
then restart your computer. Be aware that if you edit file ~/.bashrc as root, your environment variable you added will work only for root

bash script "ignores" .vimrc

I have a problem as follows:
I have a script which copies a log file from remote machine, does some modification on it and then opens it in vim, problem is vim doesn't auto recognize the file type (which outside of the script id does) – I need this for coloring the log.
Script as follows:
/usr/bin/rcp 14.1.61.10$node:/output/LocalLog_IPNode$node.log /export/home/fpd/tmp/tmp_local_log
chmod 777 /export/home/fpd/tmp/tmp_local_log/*
sed -i 's/[A-Z]\{4,8\}.*[oigus][kbdct][sel]\//---/g' /export/home/fpd/tmp/tmp_local_log/LocalLog_IPNode$node.log
vi /export/home/fpd/tmp/tmp_local_log/LocalLog_IPNode$node.log
My .vimrc:
au BufNewFile,BufReadPost LocalLog* set filetype=local_log
Note that the files opens in vim (if it helps the manual command ":set syntax=local_log" doesn't work either).
After exiting the script and manually opening the log everything works fine =(
Your problem is that the autocommand option is only availaible in vim and not vi.
So if this is available on your system, you should replace the last command line by :
vim /export/home/fpd/tmp/tmp_local_log/LocalLog_IPNode$node.log
Vim stands for "Vi Improved" and many options are only available in the latter.
To be sure you can do:
:help autocommand
It is always mentioned if the feature is vi or vim compatible.

How can I debug $PATH?

There is a directory in my $PATH that I want removed.
But I cannot figure out how it got there.
I've looked at all the places I can think of where $PATH gets set: .profile, .bashrc, .bash_profile, et cetra
but I cannot find who is putting this particular directory into $PATH.
Clearly, I do not know all the things that change $PATH when my system starts.
Is there a way to debug the startup sequence?
Ideally I just want to set a trap on anything that touches $PATH.
Or may there is a log file (or ordered set of log files) I can scan.
How can I find how each directory gets set in $PATH among all the things that run when my system starts?
There are two ways to do this:
Try bash --login -x in a terminal window. This will print every line of the setup scripts as they are executed (kudos go to kojiro).
Alternatively, you could add set -x near the top of /etc/profile or $HOME/.profile. The danger here is that if you make a mistake, your system may become unusable (if you can't start a new shell anymore) plus you need to be root. If you want to try this, I suggest to create two terminal windows, keep them open at all cost, make the changes and then start new terminal windows to see the effects.
Use grep -r PATTERN / (replace PATTERN with the path that you look for). That should search your whole hard disk for this pattern and can take a long time. Maybe start with your home directory. If you want to search just the . files, use this trick: grep -r PATTERN .??*
If you are using BASH, the way the login loading works is this order:
/etc/profile is first read in.
$HOME/.bash_profile is read in.
If $HOME/.bash_profile doesn't exist, then $HOME/.bash_login is read in.
If $HOME/.bash_login doesn't exist, then $HOME/.profile is read in.
NOTE: That $HOME/.bashrc is not read in by default. However, many people do source in their .bashrc file at login.
When a shell is invoked, and it's not a login shell, then the $HOME/.bashrc file is loaded.
By default, the Mac's PATH is /bin:/usr/bin:/usr/sbin:/sbin. This is set via /etc/profile which executes /usr/libexec/path_helper. This takes the initial paths from /etc/paths.d/... and from /etc/paths. The /etc/paths.d only adds in the /opt/X11/bin directory if X11 is installed. Otherwise, /etc/paths is used and it contains the default paths.
Mine is set to:
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin
I use /usr/local/bin for third party tools. or newer versions of already installed tools. For example, /usr/bin/svn is version 1.7.17 while /usr/local/bin/svn is set to version 1.8.10. Me invoking svn will invoke the version of Subversion I installed, and not the native version.
In this case, the 1.8 version of Subversion is actually installed in /opt/subversion/bin/svn, but I create a symbolic link to it in /usr/local/svn. I do this for any tool that I install: The tool gets installed in /opt and I link the binaries and scripts to /usr/local/bin, so they're in my $PATH.
Place these 2 lines at top of your ~/.bash_profile (create it if it doesn't exist):
set -o functrace
trap '[[ "$BASH_COMMAND" == *" PATH="* ]] && echo "$BASH_COMMAND"' DEBUG
Then exit the current shell and start a new shell.
It will print every time PATH variable is set in your environment.

~/.profile, ~/.bashrc, and ~/.bash_profile not running on new terminal start up

I am trying to create a permanent alias for my terminal. I put the alias in my ~/.profile, ~/.bashrc, and ~/.bash_profile files, previously empty. When I start a new terminal, bash does not recognize the alias, but if I source any of them, it does. Why are these not getting run when I open a terminal? I am on OSX.
Newer MacOS versions use zsh as the default shell for both Terminal and iTerm2. Run echo $SHELL to confirm if this is the case for you.
Zsh looks for a .zshrc file upon shell startup, so to continue using zsh while sourcing the contents of your bash profile, you can run the following:
echo "source ~/.bash_profile" >> ~/.zshrc
Open a new terminal window for the changes to take effect.
Two things need to happen here when using iTerm to get the loading of dotfiles to work.
First you should add the following to your .bash_profile
[[ -s ~/.bashrc ]] && source ~/.bashrc
Secondly you will need to ensure that in iTerm preferences your terminal is set to launch a login shell.
Hope this helps!
Using the default mac terminal, what worked for me was to add a command to run on start up to source my .bash_profile.
Preferences > Profile > Startup > Add command 'source ~/.bash_profile'
Mac terminal preferences window screenshot
Might be considered to be a bit hacky, but it does the trick.
Adding source ~/.profile to my .bash_profile worked for me.
As of High Sierra, both Terminal and iTerm want to load ~/.profile first. So I suggest you put one line in your .profile to make your Mac work like other Unixes:
source ~/.bash_profile
By editing this one file, you won't have to search through the menus of multiple apps to override Apple's bizarre behavior in each.
As of Catalina the default shell is now zsh. You can change it back to bash with chsh -s /bin/bash and that should load your .profile or .bash_profile
Why are your shell's initialization files not loading?
As with most things, It Depends ™
I recently experienced the same phenomenon and went through the following exercise to resolve it:
I use iTerm. iTerm runs a login shell by default. Verify in iTerm Preferences > General > Command > (*) Login Shell
Therefore, I know that ~/.bash_profile will always be called.
Knowing that, I put the following in my ~/.bash_profile file:
for file in ~/.{bashrc,bash_exports,bash_aliases,bash_functions}; do
[ -r "$file" ] && source "$file"
done
unset file
Notice that I use separate files for .bashrc, .bash_exports, etc. It keeps things separate and simple.
Note also that /etc/profile is loaded first, but since I have never used that system wide init file, I knew that that was not my problem. For details check out $ man bash
So, I started with my ~/.bash_profile file.
I found that when I installed Canopy Express that it's installer replaced the contents of my ~/.bash_profile file with the following content:
# Added by Canopy installer on 2017-04-19
# VIRTUAL_ENV_DISABLE_PROMPT can be set to '' to make the bash prompt show that Canopy is active, otherwise 1
alias activate_canopy="source '/Users/lex/dev/python/User/bin/activate'"
# VIRTUAL_ENV_DISABLE_PROMPT=1 source '/Users/lex/dev/python/User/bin/activate'
p.s. Canopy is an excellent, free python IDE, that I highly recommend.
Fortunately, I backup my ~/.bash* files so restoring that was easy and quickly fixed my issue.
My advice would be to understand the order of calls to your initialization files and start with the first one and work your way through them until you find the problem.
Oh, and you may want to verify which shell you are using (I use bash):
~ $ echo $SHELL
/usr/local/bin/bash
I am guessing you may use another shell, such as bash, tcsh, sh, zsh etc.
Put source .bash_profile into your appropriate 'bashrc' file will make the auto loading recovered, i.e.
.login for tcsh, .bash_profile for bash, .zshrc for zsh
My issue was solved by unchecking Preferences > General > tmux >
Use "tmux" profile rather than profile of the connecting session
Most likely, you need to create the files yourself as they appear not to exist by default. You should give them execute permission to make them run.
~ % sudo chmod 700 ~/.bash_profile
Also, you should check the ownership of the files. They should belong to current user rather than root. Otherwise, you will get permission denied error.
~ % ls -a -l
~ % sudo chown <user_name> ~/.bash_profile
Finally, please note that bash looks in your home directory for .bash_profile, .bash_login, and .profile in order. Bash will stop looking if the first is found.
This means if you have both .bash_profile and .profile files, the .profile will not run.
For more information
Hope this would help you.
Little late to the party but it seems that the file .zprofile is the equivalent to that of .bash_profile when loading zsh. I used this instead to execute a few commands on startup. Of course this only valid for a specific iTerm setup with zsh.
https://zsh.sourceforge.io/Intro/intro_3.html

Resources