3

Is it possible to add to the rules that bash uses for tilde expansion? I'd like to have ~data expand to /data/users/me, ~scratch expand to /data/scratch/me etc.. Is this possible, or does bash have too tight a tight hold on the '~'?

Thanks,

Andrew

ajwood
  • 18,227
  • 15
  • 61
  • 104

2 Answers2

4

~user expands to the home directory of the specified user. A nasty hack that would work is to create user named data with home directory /data/users/me etc. Adding users is distribution-specific.

moinudin
  • 134,091
  • 45
  • 190
  • 216
  • I don't have root privileges. Any way to do it without _actually_ adding users? – ajwood Jan 11 '11 at 00:16
  • @ajwood Sadly I don't think it's possible. You could try asking on http://superuser.com though, which might know of a solution. – moinudin Jan 11 '11 at 00:17
  • Assign a tilde-expanded subdirectory of "localadmin" to a variable?… `USERSUBDIRVAR=\`echo ~localadmin\`"/Subdirectory/Sub Sub Directory"` `echo "$USERSUBDIRVAR"` ➝ `/Users/localadmin/Subdirectory/Sub Sub Directory` – Alex Gray Aug 16 '12 at 00:19
4

Tilde expansion is tied to users' home directories (or the contents of the directory stack or $PWD or $OLDPWD). Use variable expansion, aliases or functions to accomplish what you're after. You can also use CDPATH to list a set of directories for cd to look in for destination directories.

Dennis Williamson
  • 346,391
  • 90
  • 374
  • 439
  • Regular variable expansion isn't as nice as the way bash handles the tilde-expansion. If I want to do a copy `cp $DATA` won't give me the files in the expanded $DATA. Doing the same operation on my home dir with `cp ~`, the result is much nicer... any suggestions? – ajwood Jan 11 '11 at 16:48
  • @ajwood: Try this: `bind '"\eL":"\e\C-e\t\t"'; data=/some/dir/you/choose/`, then: `cp $data[press Alt-Shift-L]` – Dennis Williamson Jan 11 '11 at 18:27
  • Brilliant! Thanks. Could you explain how it works, or maybe point me at the right document for me to read about it? – ajwood Jan 11 '11 at 18:57
  • @ajwood: See the "Readline Key Bindings" section of `man bash`. The `bind` command above is binding Meta-Shift-L (Alt-Shift-L) (Escape/Meta is `\e`) to the keystroke for the readline function `shell-expand-line` plus two tabs. That function expands the variable (and possibly other things on your command line) and the tabs initiate completion as the would if you typed them yourself. You could press Ctrl-Alt-e yourself, by the way, to get the expansion by itself. – Dennis Williamson Jan 11 '11 at 19:04
  • Thanks, I'll take a look at that. It expands most of the time, but doesn't when the variable is an arg to `cd'. Is that expected? – ajwood Jan 11 '11 at 20:42
  • @ajwood: It works for me. Do you have bash-completion installed (it shouldn't make a difference either way)? The `complete` command with no arguments will list a bunch of entries (585 on my system) if you do. To see if it's enabled, `shopt -p progcomp` should include "-s" in its output. Again, that shouldn't make a difference. Does the variable name just sit there when you try to expand it - and only with `cd`? Try it with `pushd` and see if it works. – Dennis Williamson Jan 11 '11 at 21:04
  • @ajwood: Here's another neat trick (no dollar signs in the following): `shopt -s cdable_vars; varname=/some/dir; ls varname` "no such file or directory" or it lists a non-directory file `cd varname` - you're now in `/some/dir`. – Dennis Williamson Jan 11 '11 at 21:05