Skip to content

Instantly share code, notes, and snippets.

@jaapie

jaapie/up.sh

Last active Dec 14, 2015
Embed
What would you like to do?
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
You can’t perform that action at this time.