EzDevInfo.com

.bash-profile interview questions

Top .bash-profile frequently asked interview questions

How can you remove duplicates from bash history?

Since I'm saving history from different sessions this is an issue where erasedups can't help because I'm using the following:

PROMPT_COMMAND="$PROMPT_COMMAND;history -a"

Is there an easy way to delete duplicates in history?


Source: (StackOverflow)

MinGW Bash profile

I use MinGW on windows 7. I have a .bashrc with some aliases in it. The file is in my home folder which is where MinGW starts me in, so it also believes that the folder is my home folder. It does not load the contents of the folder automatically. I have to run the bash command to get the aliases to work.

I have tried renaming it to .bash_profile. This only made things worse as it didn't load automatically and also didn't load when I ran bash manually.

How can I fix this problem?


Source: (StackOverflow)

Advertisements

~/.profile is not loaded when using SSH (Ubuntu)

Edited to reflect the problem I really wanted to solve:

I need to set up my ruby environment so I can deploy via Capistrano.

export PATH=$HOME/.rbenv/bin:$PATH
eval "$(rbenv init -)"

I put these in ~deploy/.profile, but when I ssh in, they aren't being run. Ideas?

I'm running Ubuntu 12.04.


The original question was:

When I ssh into another account at localhost, it doesn't load my .profile. How can I force ssh to load it? I'm running Ubuntu 12.04.


Source: (StackOverflow)

Difference between .bashrc and .bash_profile

What's the difference between .bashrc and .bash_profile and which one should I use?


Source: (StackOverflow)

Where to find the .bashrc file on Mac OS X Snow Leopard and Lion?

I want to install rvm on my Snow Leopard machine.

It says I need to add a line to my .bashrc file (I'm using bash) but where is my .bashrc file?


Source: (StackOverflow)

How do I modify my git bash profile in Windows?

I'm using Git Bash on Windows 7 and would like to set up bash profile aliases such as alias gs='git status' to make my life easier. How can I do this?


Source: (StackOverflow)

Environment variables in bash_profile or bashrc?

I have found this question [blog]: Difference between .bashrc and .bash_profile very useful but after seeing the most voted answer (very good by the way) I have further questions. Towards the end of the most voted, correct answer I see the statement as follows :

Note that you may see here and there recommendations to either put environment variable definitions in ~/.bashrc or always launch login shells in terminals. Both are bad ideas.

  1. Why is it a bad idea (I am not trying to fight, I just want to understand)?

  2. If I want to set an environment variable and add it to the PATH (for example JAVA_HOME) where it would be the best place to put the export entry? in ~/.bash_profile or ~/.bashrc?

  3. If the answer to question number 2 is ~/.bash_profile, then I have two further questions:

    3.1. What would you put under ~/.bashrc? only aliases?

    3.2. In a non-login shell, I believe the ~/.bash_profile is not being "picked up". If the export of JAVA_HOME entry was in bash_profile would I be able to execute javac & java commands? Would it find them on the PATH? Is that the reason why some posts and forums suggest setting JAVA_HOME and alike to ~/.bashrc?

    Thanks in advance.


Source: (StackOverflow)

How can I fix a Broken Pipe error?

I recently reinstalled RVM (following the instructions at http://rvm.io) after a fresh install of Ubuntu 12.10 when I got an SSD Drive.

Now, when I type: type rvm | head -1

I receive the following error:

rvm is a function
-bash: type: write error: Broken pipe

But if I immediately repeat the command then I only receive:

rvm is a function

And it appears everything is ok? What's happening? What can I do to fix it? It doesn't happen always. It appears to be more sporadic. I've tried to find some kind of pattern to it but haven't yet.


Source: (StackOverflow)

enabling colors in the python interactive shell on OS/X

I have just modified my .bash_profile so as to have colors in iTerm and Terminal.

Now, I want to enable syntax highlighting for the Python interactive shell, IDLE-style.

How to do that?


Source: (StackOverflow)

Syncing bash profile between computers

I have tons of stuff in my .bash_profile. The problem is, I use ~3 computers very frequently, and I'm tired of having to copy paste my prefs everywhere. Two of them run Ubuntu 10.10, and one runs OSX. I was wondering if there was a way to use Dropbox, to share a single prefs file. Like, when bash starts, tell it to check ~/Dropbox/Bash/.bash_profile ?

Although, could I also tell emacs to look in ~/Dropbox/Emacs/.emacs somehow?


Source: (StackOverflow)

Is it possible to override the command line's built in "cd" command?

Just about every time I 'cd' to a different directory on my machine (in this case, running Mac OS X 10.6.7) via the command line (which is bash), I immediately type 'ls' to get the list of contents in that directory. I'm trying to figure out a way to override 'cd' so that it changes to the requested directory and then gives me the list in one shot.

I've been able to get the basic functionality I'm looking for working by with the following line added to my ~/.bash_profile

function cl() { cd "$@"; ls -l; }

This works as expected. Changing to the requested directory and then showing me the contents. Where I run into an issue is trying to override "cd" itself instead of creating a new "cl" command.

The following things do not work

##### Attempt 1 #####
# Hangs the command line

function cd() { cd "$@"; ls -l; }


##### Attempt 2 #####
# Hangs the command line

function cd() { 'cd' "$@"; ls -l; }


##### Attempt 3 #####
# Does not change directory. 
# Does list contents, but of the directory where you started.

function cd() { /usr/bin/cd "$@"; ls -l; }


#### Other attempts that fail in various ways #####
alias cd=cd "$@"; ls -la;
alias cd="cd '$@'; ls -la;"
alias cd='cd "$@"'; ls -la;
alias cd=/usr/bin/cd "$@"; ls -la;

I also tried several other iterations that aren't listed as well as making an alias that points to the working 'cl' function. None of which worked.

What I've read in documentation talks about the fact that 'cd' can't be run as an external command (which is what I understand to be the way the function would need to use it).

So, I can currently use my "cl" command and get what I want, but the question is/remains:

Is there a way to override the behavior of 'cd' to have it change to the requested directory and then do something else afterward?


Source: (StackOverflow)

How to automatically execute a shell script when logging into Ubuntu

How do I get a script to execute automatically when I log in? Not when the machine starts up, and not for all users, but only when I (or any specific user with the script) login via the GNOME UI.

From reading elsewhere I thought it was .bash_profile in my home directory, but for me it has no effect. When I manually execute it in a terminal window by typing ~/.bash_profile it works, but it won't run automatically when I log in.

I'm running Ubuntu 11.04. The file permission on my .bash_profile is -rwx------. No .bash_profile existed in my home directory before I created it today.

I seem to remember older versions of Linux having a .profile file for each user, but that doesn't work either.

How is it done? Do I need to configure something else to get the .bash_profile to work? Or does the per-user login script need to be in some other file?


Source: (StackOverflow)

How to reset Bash on Mac OSX, .bash_profile corrupted and bash no longer works

I am on a MacBook Pro, running the latest version of Mountain Lion.

I really need some help, I have managed some how to damage my .bash_profile (I think) so that every time I open up the terminal I get the error listed below.

-bash: export: `/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/go/bin': not a valid identifier
-bash: export: `/Users/rob/Applications/sbt/bin:': not a valid identifier
env: bash: No such file or directory
env: bash: No such file or directory
env: bash: No such file or directory
env: bash: No such file or directory
env: bash: No such file or directory
-bash: tar: command not found
-bash: grep: command not found
-bash: cat: command not found
-bash: find: command not found

I am not sure what has happened, I have no sudo, cd or any normal commands. The only way I have been able to get to any of the main directories is through the go to folder command in finder and try to find the file to no avail.

To top it all off I think I created a file that might be causing the issue, I wanted to edit the .bash_profile so I typed

sudo nano ./bash_profile

This open a new file in nano which I think was then saved. After this I opened the real .bash_profile to add in the path for node.js.

If I can get to the .bash_profile I think I can get it back on track but I can't find it, should I reinstall bash? If so how would I do that on a mac, I tried using

brew install bash

to which I get

-bash: brew: command not found

I am really stuck if anyone can help I would be really appreciated.

Many thanks


Source: (StackOverflow)

why error code 1 returned by loading .bash_profile in Mac

Whenever I open a bash terminal, the error code 1 is returned:

Last login: Tue Jan 15 16:19:53 on ttys000
spam@moss:~ $ echo $?
1

I found this is caused by last line of code in my .bash_profile:

test -f $HOME/.debug && export profile_bash_profile='.bash_profile' || return 0

If I remove this line, then the error code 0 is returned. I don't understand how this line could cause any problem, because I can source .bash_profile with the error code 0:

spam@moss:~ $ source .bash_profile
spam@moss:~ $ echo $?
0

Update:

Does anyone one know how Mac load ~/.bash_profile? I doubt Mac sources it when a login shell is launched. It seems the return command doesn't run as expected - when I put return 5 as the last line of ~/.bash_profile and launch a login shell, it does not return error code 5 (and I am sure ~/.bash_profile is the last script loaded).


Source: (StackOverflow)

Mac OSX: Cannot color broken symlinks

On my linux machine, you can tell intact symlinks from broken ones, because broken ones are colored red. I've tried to set this up on my mac by adding some configurations to PSCOLORS in my .bash_profile, but so far nothing has worked. I've managed to get the coloring to tell apart directories, files, executables, and symlinks. However, the broken symlinks are not a different color than intact symlinks. Searching around on google hasn't rendered any promising results. Ideas? Tricks?


Source: (StackOverflow)