Removing a letter from a command's first argument

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
kennedyri [ Fr, 26 Oktober 2007 22:37 ] [ ID #1854905 ]

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

On 2007-10-26, kennedyri [at] 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 <http://cfaj.freeshell.org/shell/>
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
cfajohnson [ Sa, 27 Oktober 2007 00:27 ] [ ID #1855688 ]

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

> 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
Jstein [ Do, 01 November 2007 11:01 ] [ ID #1859598 ]
Linux » comp.unix.shell » Removing a letter from a command's first argument

Vorheriges Thema: Can you do the following
Nächstes Thema: Manage date in bash