Tag Info

Hot answers tagged

82

I have a little script that extracts archives, I found it somewhere on the net: extract () { if [ -f $1 ] ; then case $1 in *.tar.bz2) tar xvjf $1 ;; *.tar.gz) tar xvzf $1 ;; *.bz2) bunzip2 $1 ;; *.rar) unrar x $1 ;; *.gz) gunzip $1 ;; ...


40

Since I use so many different machines, my .bashrc always sets the command prompt to include, among other things, the name of the server I am currently logged into. This way, when I am three levels deep in telnet/ssh, I don't type the wrong thing in the wrong window. It really sucks to rm -rf . in the wrong window! (Note: At home, telnet is disabled on ...


30

ssh -t username@hostname /bin/sh works for me.


26

Color for manpages in less makes manpages a little easier to read: export LESS_TERMCAP_mb=$'\E[01;31m' export LESS_TERMCAP_md=$'\E[01;31m' export LESS_TERMCAP_me=$'\E[0m' export LESS_TERMCAP_se=$'\E[0m' export LESS_TERMCAP_so=$'\E[01;44;33m' export LESS_TERMCAP_ue=$'\E[0m' export LESS_TERMCAP_us=$'\E[01;32m' Colored manpages can also be obtained by ...


25

No more cd ../../../.. but up 4 Goes up many dirs as the number passed as argument, if none goes up by 1 by default (found in a link in a comment in stackoverflow.com and modified a bit) up(){ local d="" limit=$1 for ((i=1 ; i <= limit ; i++)) do d=$d/.. done d=$(echo $d | sed 's/^\///') if [ -z "$d" ]; then d=.. fi cd $d ...


25

you can try to abort (ctrl+C) before the exit part of your .bashrc is executed. I tried by adding the following at the top of a testuser's bashrc, it works, it's just a matter of timing. Very easy in my case: sleep 3 echo "Too late... bye" exit 0


19

I deal with a lot of different machines so one of my favorites is aliases for each machine that I need to frequently SSH to: alias claudius="ssh dinomite@claudius" It is also useful to setup a good .ssh/config and ssh keys to make hopping amongst machines even easier. Another one of my favorite aliases is for moving up directories: alias ..="cd .." ...


17

It's copied from /etc/skel/.bashrc for new users


15

GPG encrypted bashrc I'm sure we all have things we'd like to put in our bashrc that we don't want easily readable by sudoers. My solution to this is: if [ -f ~/.bash_private.gpg ]; then eval "$(gpg --decrypt ~/.bash_private.gpg 2>/dev/null)" fi I have a GPG agent that makes it so I only have to enter my private key's password once every few hours. ...


15

I haven't used cygwin in some time, but I'm guessing that it wants ~/.bash_profile. Simple fix to test. ln -s ~/.bashrc ~/.bash_profile Or if ~/.bash_profile exists, source .bashrc. if [ -f ~/.bashrc ] then . ~/.bashrc fi


12

this is an awesome resource for this: show us your .bashrc


12

I used to set these up all over the place but then realized that it was better to just remember how to do them 'manually' because it meant I would 1) fully understand what was going on and 2) have access to these capabilities even if my custom .bashrc wasn't installed. The only thing I use aliases for these days are to cut down on repetitive typing of ...


12

In your login shell's profile files you can set up some things that you will use during your session, and which are needed to be done only once. Some ideas: create a temporary file containing the ip you connected, later on you can include it in some scripts setting firewall rules. run ssh-agent, ask for your ssh keys, and store the ssh agent environment ...


10

I usually address this with a short cron wrapper script: #!/bin/bash [ -r $HOME/.bashrc ] && . $HOME/.bashrc [ -r $HOME/.profile ] && . $HOME/.profile exec "$@" Then just prefix the command in crontab with your wrapper: * * * * 1-5 ~/scripts/cron-wrapper ~/scripts/myscript.sh * * * * 1-5 ~/scripts/cron-wrapper ~/scripts/myotherscript.sh ...


9

The one liners and tiny scripts out there could go on forever. I recommend man bash and writing things yourself. Some good short bash stuff at http://www.commandlinefu.com. Heres a few things. #use extra globing features. See man bash, search extglob. shopt -s extglob #include .files when globbing. shopt -s dotglob #When a glob expands to nothing, make it ...


9

A little tip for Bash if you are a sysadmin and work with root privileges a lot: shopt -o noclobber This will prevent you from accidentally destroying the content of an already existing file if you redirect output (>filename). You can always force overwriting with >|filename.


9

I think your only options are: ssh in as another user and su to your account; use something like ftp or smbclient, if the relevant services are enabled on the host; find an open vulnerability in an open network service and exploit it :). get an admin to fix the problem.


9

mv ~/.bashrc ~/.bashrc.messed cp /etc/skel/.bashrc ~/.bashrc


9

Try doing this instead if [ "$SSH_TTY" ] then source .bashc_real fi


8

I have the following in my bashrc function __setprompt { local BLUE="\[\033[0;34m\]" local NO_COLOUR="\[\033[0m\]" local SSH_IP=`echo $SSH_CLIENT | awk '{ print $1 }'` local SSH2_IP=`echo $SSH2_CLIENT | awk '{ print $1 }'` if [ $SSH2_IP ] || [ $SSH_IP ] ; then local SSH_FLAG="@\h" fi PS1="$BLUE[\$(date +%H:%M)][\u$SSH_FLAG:\w]\\$ ...


8

Usually you can tell bash to ignore these rc files with: ssh -t your-host bash --noprofile --norc After login, you can view and update the problematic rc files.


7

Use the pathmunge() function available in most distro's /etc/profile: pathmunge () { if ! echo $PATH | /bin/egrep -q "(^|:)$1($|:)" ; then if [ "$2" = "after" ] ; then PATH=$PATH:$1 else PATH=$1:$PATH fi fi } edit: For zsh users, typeset -U <variable_name> will deduplicate path entries.


7

Move the damaged file out of the way, then reinstall the package that provides the damaged file. mv /etc/bashrc /etc/bashrc.damaged yum reinstall $(rpm -qf /etc/bashrc)


6

I've had this in my .bashrc for a while and I've found it helpful. If you are sshing in to the box, is starts screen automatically when you login, that way when your network connection gets interrupted or whatever, you don't lose whatever you were doing. It should be placed at the end. if [ "$PS1" != "" -a "${STARTED_SCREEN:-x}" = x -a "${SSH_TTY:-x}" != x ...


6

It depends somewhat on how you personally use shells. An interactive shell is anything that has a terminal connected to its input and output. Login shells are spawned by /bin/login. Login shells source your .bash_profile. Most terminal emulators such as xterm start an interactive shell that is not a login shell. Interactive, non-login shells source your ...


6

Add your modified PS1 setting to /etc/profile.d/custom_ps1.sh. Files under /etc/profile.d are automatically sourced from /etc/profile: if [ -d /etc/profile.d ]; then for i in /etc/profile.d/*.sh; do if [ -r $i ]; then . $i fi done unset i fi Which is called whenever a login shell is spawned. From the bash manpage: When bash is ...


5

How many aliases to fortune do you need, anyway? I like to make a cdd alias that takes me to wherever I'm presently most likely to be working on that server. PATH redefinition really belongs in .bash_profile, not .bashrc. On a server where I routinely use a large set of screens, my .bashrc will have: alias s1="screen -dr chaos1" alias s2="screen -dr ...


5

Among other things, I set some defaults for less, prevent accidentally closing my terminal and enable forward navigation through history: # ignore case, long prompt, exit if it fits on one screen, allow colors for ls and grep colors export LESS="-iMFXR" # must press ctrl-D 2+1 times to exit shell export IGNOREEOF="2" # allow ctrl-S for history navigation ...


5

I have a few bits: # stop the pc speaker ever annoying me :) setterm -bfreq 0 # don't put duplicate lines in the history. See bash(1) for more options HISTCONTROL=ignoredups # ... and ignore same sucessive entries. HISTCONTROL=ignoreboth # Expand the history size HISTFILESIZE=10000 HISTSIZE=100 # commands with leading space do not get added to history ...


5

There's a bash built-in test for TTY. I forget when it was added, 3.0? I believe it's relatively new. I use it in scripts where I need different behavior when it's run from cron or a user runs it directly. if [ -t 0 ]; then echo "I'm a TTY" fi



Only top voted, non community-wiki answers of a minimum length are eligible