Skip to content

Instantly share code, notes, and snippets.

@jaapie
Last active December 14, 2015 19:59
Show Gist options
  • Save jaapie/5140328 to your computer and use it in GitHub Desktop.
Save jaapie/5140328 to your computer and use it in GitHub Desktop.
A shell function for Bash that goes up a specified number of directory levels. Easier than typing `cd ../../../` etc.
#! /bin/bash
function up () {
levels=$1
if [ -z "$levels" ]; then
levels=1
fi
# Test if $levels is a number; the -eq operator expects a number, and will
# output an error if one is not found. Any output the STDERR is redirected
# to the bit bucket (/dev/null)
if [ "$levels" -eq "$levels" ] 2> /dev/null; then
if [ "$levels" -eq "0" ]; then
levels=1
fi
for (( c=1; c<=levels; c++ ))
do
cd ../
done
else
echo up: expected a number, not $levels
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment