How to reset bash config? [closed] - linux

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
Previously, when I mistakenly entered a wrong command in the terminal, I would get an output like this:
Command 'whoaim' not found, did you mean:
command 'whoami' from deb coreutils (8.30-3ubuntu2)
Try: sudo apt install <deb name>
but now when I enter a wrong command like whoaim instead of whoami I get this output:
bash: whoaim: command not found
how should I reset bash config?

You can reset the bash profile of user using following commands:-
Overwrite the existing .bashrc from user's home directory.
cp /etc/skel/.bashrc ~/
source ~/.bashrc
Take backup of existing .bashrc from user's home directory
However, check here how you can use the utility command-not-found in ubuntu

You would need to call a function that gives bash this capability. It's typically called command_not_found_handle. First, append it to your .bashrc file. Here is the code:
command_not_found_handle ()
{
if [ -x /usr/lib/command-not-found ]; then
/usr/lib/command-not-found -- "$1";
return $?;
else
if [ -x /usr/share/command-not-found/command-not-found ]; then
/usr/share/command-not-found/command-not-found -- "$1";
return $?;
else
printf "%s: command not found\n" "$1" 1>&2;
return 127;
fi;
fi
}
And then run this command to reload the profile file:
. ~/.bashrc

Related

How to use 'history-c' command in a bash script? [closed]

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 7 years ago.
Improve this question
As we know, 'history' command displays the command line history of Linux server and 'history -c' is the command to clear/delete this command line history.
I have to trigger this command through my bash script. Script is as follows,
#! /bin/bash
var=`history -c`
if [ $? -eq 0 ]
then
echo "cleared"
echo $var
fi
Output is as follows:
cleared
Though its printing "cleared" as the output, history-c is not deleting the history.
It would be great if you can guide/suggest on how i can achieve this, i.e using "history-c" command in my bahs script to delete command line history.Or is there any other way in which i can delete command line history through my bash script.
Thanks & Regards,
Navya
history -c clears the history for the current shell, and does not delete ~/.bash_history.
But when you run a script the current shell creates a new shell to run the script in and exits that shell when the script is done.
Instead, to execute a script in the current shell you have to source the script. If the name of your script is foo.sh, try running . ./foo.sh
But in either case, the script that you've written does not execute the command. Modify it to something like this:
#! /bin/bash
history -c
if [ $? -eq 0 ]
then
echo "cleared"
fi

How to quickly move to a real directory using a soft link directory in linux? [closed]

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 7 years ago.
Improve this question
I have a dir /var/real-dir
I've created a soft link to it like this
ln -s /var/realdir /var/virtual-dir
Having that my working directory is /var/virtual-dir, I'm searching for a way to cd to real-dir with as less typing as possible.
You can use cd -P .
Note that this only updates the PWD and OLDPWD environment variables; the kernel-level current directory remains unchanged.
Alternatively, you can use the -P option with the initial cd like cd -P /var/virtual-dir.
You can:
cd "$(readlink -f .)"
If this is too much typing, you can create a helper function in your .bashrc, like this:
function cdlink() {
cd "$(readlink -f .)"
}
source ~/.bashrc or start a new shell and can simply type:
cdlink

linux "lessPATH" similar to $CDPATH and $PATH [closed]

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 8 years ago.
Improve this question
I like the flexibility and how easy it is to execute commands from any directory in $PATH variable or $CDPATH to change directory.
But is there an easy way to "less or open a file" from a list of directories you often visit.
Say you have log files in a different dir and other frequently visited directories in another. A less file1 command should look for the file1 in a list of directories defined like a $PATH or $CDPATH variable.
I understand that you look for an existing solution but if you use bash you can write a function than has exactly this behavior :
put in ~/.bashrc:
less2()
{
if [ $# -eq 0 ]; then
echo 'Missing filename ("less --help" for help)'
return 1
fi
if [ "$1" == "--help" ]
then
less $1
return 1
fi
OLDIFS=$IFS
IFS=':'
if [ -z $LESSPATH ]; then
SEARCH_PATHS=.
else
SEARCH_PATHS=.:${LESSPATH}
fi
for dir in $SEARCH_PATHS
do
if test -e "$dir/$1"
then
less "$dir/$1"
IFS=$OLDIFS
return
fi
done
IFS=$OLDIFS
echo "$1: No such file or directory"
}
You need to execute source ~/.bashrc in order to get less2 in your bash.
How to use the script
By defaults it looks for files in a current directory. If you set the enviroment variable LESSPATH less2 will look for a file first in the current directory and then if it is not there it will look for the file in all the directories in $LESSPATH. less2 is a function in a current bash process so it is not necessary to export LESSPATH, but of course you can also export LESSPATH.
$ less2 my_file.log
$ LESSPATH=path1:path2:path3
$ less2 my_other_file.log

Change cd default directory (bash) [closed]

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 8 years ago.
The community reviewed whether to reopen this question 9 months ago and left it closed:
Original close reason(s) were not resolved
Improve this question
I'm looking for a way to change the default directory of cd, and I'm wondering if this is possible. I tried adding
alias "cd=cd ~/Documents/Github"
to .bashrc, but this obviously doesn't work because it breaks the cd command so that you cannot use cd for anything but going to that directory. Is there some export that I could put in .bashrc to do this? I do not want to change my home directory, just the default directory cd goes to. I ask because I regularly use cd to change to my default directory that I am programming in and would like to not have to type cd ~/workspace or cd and then cd workspace every time I want to change to the directory ~/workspace.
Here's a simpler alternative using an alias:
alias cd='HOME=~/Documents/Github cd'
While this redefines $HOME, it does so ONLY for the cd command, so should be safe(*).
This also obviates the need for any custom parameter pre-parsing.
If you place this in ~/.bashrc, you should get the desired behavior.
Note that, by default, this alias will NOT be in effect in scripts (non-interactive shells), as alias expansion is by default disabled there (use shopt -s expand_aliases to explicitly enable).
(*) #chepner points out one restriction: with the alias in place you won't be able to do HOME=/somewhere/else cd, i.e., you won't be able to redefine (override) $HOME again, ad-hoc. As he further states, though, it's hard to imagine why anyone would want to do that.
You can shadow the builtin cd command with a shell function. Add the following to your .bashrc file.
cd () {
if [ $# = 0 ]; then
builtin cd ~/Documents/Github
else
builtin cd "$#"
fi
}
This isn't perfect, as it assumes you will never call the function as
* cd -L
* cd -P
* cd -LP
* etc
(that is, using one or more of the supported options without an explicit directory.)
UPDATE
This might be a little more comprehensive, but I haven't tested it.
cd () {
local -a args
while getopts LP opt "$#"; do
case $opt in
-*) args+=( "$opt" ) ;;
esac
done
shift $((OPTIND-1))
# Assume at most one directory argument, since
# that is all cd uses
args+=( "${1:-~/Documents/Github}" )
builtin cd "${args[#]}"
}
this is default home location
$ pwd
/home/######
now you can reset it like this
$ export HOME=/tmp
$ cd
$ pwd
/tmp
now it's up to you where to put the new $HOME definition - .bashrc, or whatever

changes in bash_profile not getting reflected linux [closed]

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 8 years ago.
Improve this question
Below is the .bash_profile file that I have edited. The changes I make here are not getting reflected when I use echo $JAVA_HOME or echo $PATH.
When I use $PATH, I get /usr/lib64/qt-3.3/bin:/usr/local/bin:/bin:/usr/bin which is not found in any of the .bash_profiles or .bash_rcs.
How can I make my .bash_profile work?
# .bash_profile
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
# User specific environment and startup programs
export PATH=/sbin/:$PATH
export PATH=$PATH:$HOME/bin
export JAVA_HOME=/export/home/lg199447/dev/jdk1.7.0_51/bin/java
export PATH=$PATH:/export/home/lg199447/dev/jdk1.7.0_51/bin
Note:
I am trying to login to a server from an OS X terminal using ssh and once I'm logged in my terminal was showing $ followed by my cursor. I was unable to use my arrow keys and tab. So I manually stared bash by executing bash in /bin directory. This changed my terminal as lg199447#VDCALD564 /]$ and I was able to use terminal in a normal way I use in mac.
This sounds like you login shell on the linux machine is not bash, but some other shell variant. ~/.bash_profile is only sourced for bash login shells, so if you just execute bash, it's not.
Either make /bin/bash your login shell (using the command chsh -s /bin/bash), or start bash using bash -l, then it should work.
Another option would be to place your startup code in ~/.bashrc, which is sourced for for all interactive bash sessions (except if explicitly disabled with the --norc option).

Resources