Hot answers tagged

81

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 ;; *....


57

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


39

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 ...


32

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


25

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 ...


24

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 ...


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 .." ...


19

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


17

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


16

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


16

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


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 used a published CVE to execute a command as root through a web interface in a network monitoring software I had installed. "rm /RAID/home/tom/.bashrc" Then I could login and svn revert the changes I made.


13

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.


13

Use ~/.bash_completion From the Bash Completion FAQ: Q. How can I insert my own local completions without having to reinsert them every time you issue a new release? A. Put them in ~/.bash_completion, which is parsed at the end of the main completion script. See also the next question. Q. I author/maintain package X and would like to ...


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 only need to be done once. Some ideas: create a temporary file containing the IP address you connected from, 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 ...


11

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 ...


11

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.


11

I think sshrc is what you're looking for: https://github.com/Russell91/sshrc sshrc works just like ssh, but it also sources ~/.sshrc after logging in remotely. $ echo "echo welcome" > ~/.sshrc $ sshrc me@myserver welcome $ echo "alias ..='cd ..'" > ~/.sshrc $ sshrc me@myserver $ type .. .. is aliased to `cd ..' You can use this to set ...


10

Could you describe how do you test these 2 files? Did you try login or non-login shell? Here is the difference: When you login your system and see the command line prompt, it’s a login shell, and it executes these files in order: /etc/profile ~/.bash_profile ~/.bashrc /etc/bashrc A non-login shell will only execute the two files in order: /etc/bashrc ~/....


10

First off, never use root to execute day-to-day commands. That's the best way to actually expose yourself to disasters. With that in mind, if you use sudo, you can actually limit commands AND the command options that a user can execute with sudo. For example, in your sudoers file, you can limit using rm like so: myuser ALL=(root) NOPASSWD: rm -r This ...


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 actually discovered a fairly elegant solution by adding the '-l' (--login) flag to my bash command, which causes it to source all login files, including .bashrc. Hence my crontab command is simply: * * * * 1-5 /usr/bin/bash -lc '/mnt/group/core/deploy/scripts/test.sh' > /dev/null 2>&1


9

Mike's answer will probably work. But it's worth pointing out that you can accomplish this carefully selecting which startup files to put the verbose stuff in. From the bash man page: When bash is invoked as an interactive login shell, or as a non-interactive shell with the --login option, it first reads and executes commands from the file /etc/...


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

I managed to mess up my .bashrc file too on a new cluster I've been given trial access to. Not wanting to seem like a noob, the last thing I wanted to do was ask for help from the admins, and I couldn't get a well-timed ^+C to work. What did work however, was to send an 'rm' command as a final argument to ssh. i.e. ssh -tv user@host rm .bashrc I couldn't ...


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.



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