// Internet Duct Tape

How to embed single quotes in a tcsh alias

Posted in Asides, Linux and Unix, Technology by engtech on May 25, 2006

When writing aliases there is a mechanism for including single quotes within the alias.

alias test 'echo put some single quotes here '"'"'this is quote"'"'" there were quotes'

This makes is useful for creating one line perl aliases. The following aliases could have probably been done more tersely with sed, but this format will be easier to understand if you're familiar with perl.

# Returns the current directory in my user account.
alias myd 'perl -w -e '"'"'use strict; use Cwd;   my $home = "/home/";   my $pwd = cwd() . "/";   $pwd =~ s/$home.*?\//$home$ENV{USER}\//;   print $pwd;'"'"''
# Returns the current directory on another user account.
alias otherd 'perl -w -e '"'"'use strict; use Cwd;   my $other_user = $ARGV[0];  (defined $other_user) || die "alias otherd: specify a user account!";  my $home = "/home/";   my $pwd = cwd() . "/";   $pwd =~ s/$home.*?\//$home$other_user\//;   print $pwd;'"'"''
# These aliases assume an option called e that launch a text editor. 

# Open the same file under my directory.
alias myf 'e `myd`\!*'
# Open the same file under someone else's directory.
alias otherf 'e "`otherd \!:1`\!:2"'

Comments Off on How to embed single quotes in a tcsh alias

%d bloggers like this: