Removing a letter from a command"s first argument

Removing a letter from a command"s first argument

am 26.10.2007 22:37:28 von kennedyri

When I'm changing directories, instead of something like "cd /opt",
I'll sometimes make a mistake and type it as "c d/opt" instead. I've
been making this typo for years, so I don't think it's a habit I'm
going to unlearn soon. My idea is to make an alias for "c" to run "cd"
with the leading "d" removed from the first argument. I'm using tcsh,
and I'm at a loss for how to change the first argument.

Is an alias the right way to go? How can I do this?

--
Rob

Re: Removing a letter from a command"s first argument

am 27.10.2007 00:27:43 von cfajohnson

On 2007-10-26, kennedyri@gmail.com wrote:
>
>
> When I'm changing directories, instead of something like "cd /opt",
> I'll sometimes make a mistake and type it as "c d/opt" instead. I've
> been making this typo for years, so I don't think it's a habit I'm
> going to unlearn soon. My idea is to make an alias for "c" to run "cd"
> with the leading "d" removed from the first argument. I'm using tcsh,
> and I'm at a loss for how to change the first argument.
>
> Is an alias the right way to go? How can I do this?

That's simple to do in a POSIX shell (sh, bash, ksh, etc) with a
function (note: this version doesn't deal with options):

c()
{
cd "${1#d}"
}

Note however, that it fails if you do: c data
Instead, use: c ./data

--
Chris F.A. Johnson, author
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
===== My code in this post, if any, assumes the POSIX locale
===== and is released under the GNU General Public Licence

Re: Removing a letter from a command"s first argument

am 01.11.2007 11:01:00 von Jstein

> Note however, that it fails if you do: c data
> Instead, use: c ./data

You could prevent it from failing, by testing to see if $1 starts
with the pattern d/ or d. even by looking to see if a DIR exists
that matches a name starting with the 2nd char of $1

c()
{ case "$1" in
d) shift; _toDir="${1}" ;; #-- 'd' was alone, target
dir probably in $2
d?*) _toDir="${1#d}" ;; #-- 'd' attached to target
dir, clip 1 char from front of $1
esac

cd "${_toDir}"
}

#-- It might be worth a try