# expanding alias from a variable

**URL:** https://community.unix.com/t/expanding-alias-from-a-variable/250267
**Category:** Shell Programming and Scripting
**Created:** [September 29, 2009, 9:40am UTC](https://community.unix.com/t/expanding-alias-from-a-variable/250267 "2009-09-29T09:40:50Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![locutus01](https://community.unix.com/letter_avatar/locutus01/32/5_5575768a8748004e209b776fc1b2916d.png) [@locutus01](https://community.unix.com/u/locutus01)
#### Post date: [September 29, 2009, 9:40am UTC](https://community.unix.com/t/expanding-alias-from-a-variable/250267/1 "2009-09-29T09:40:50Z")

</div>

Hi !

I am making my first steps to make a script. Therefore i try to make a scp command more easier. Given is the following alias:

14='admin@x-abcd-def.xyz

Now i want to let the script read three var's from the console to use them in the script and then build the scp string.

```nohighlight
echo "Servernumber:";
read srv="";
echo "remotefile incl. abs. path";
read remotefile="";
echo "localfile incl. abs.path"
read localfile

scp admin@$srv:$remotefile $localfile

```

Doing it this way $srv is not expanded to the value of the alias. expand\_alias is set to on in the bash. So where is my mistake?

---

<div class="post-metadata">

### Author: ![dr.house](https://community.unix.com/letter_avatar/dr.house/32/5_5575768a8748004e209b776fc1b2916d.png) [@dr.house](https://community.unix.com/u/dr.house)
#### Post date: [September 29, 2009, 10:19am UTC](https://community.unix.com/t/expanding-alias-from-a-variable/250267/2 "2009-09-29T10:19:50Z")

</div>

```nohighlight
#! /bin/bash

echo -n "Server no.: " ; read server
echo -n "Remote file: " ; read remote
echo -n "Local file: " ; read local

echo "SCP:> $server $remote $local"

exit 0

```

```nohighlight
[house@leonov] sh test.bash
Server no.: 12
Remote file: this
Local file: that
SCP:> 12 this that

```

---

<div class="post-metadata">

### Author: ![peterro](https://community.unix.com/letter_avatar/peterro/32/5_5575768a8748004e209b776fc1b2916d.png) [@peterro](https://community.unix.com/u/peterro)
#### Post date: [September 29, 2009, 10:24am UTC](https://community.unix.com/t/expanding-alias-from-a-variable/250267/3 "2009-09-29T10:24:38Z")

</div>

Your code isn't consistent even with similar lines. Remove the semi-colons from the end of the lines. Your 'read' lines aren't the same throughout the script either:

```nohighlight
#!/bin/sh

echo "Servernumber:"
read srv
echo "remotefile incl. abs. path"
read remotefile
echo "localfile incl. abs.path"
read localfile

scp admin@$srv:$remotefile $localfile

```

A good method for testing techniques/syntax is to pull just the technique out into a simple 2-3 line script to validate that the code works and then put it into your script.

Something like:

```nohighlight
read something
echo $something

```

or

```nohighlight
srv=box
remotefile=/file
localfile=/another/file
scp admin@$srv:$remotefile $localfile

```

---

<div class="post-metadata">

### Author: ![locutus01](https://community.unix.com/letter_avatar/locutus01/32/5_5575768a8748004e209b776fc1b2916d.png) [@locutus01](https://community.unix.com/u/locutus01)
#### Post date: [September 29, 2009, 5:54pm UTC](https://community.unix.com/t/expanding-alias-from-a-variable/250267/4 "2009-09-29T17:54:41Z")

</div>

> [@dr.house](#):
>
> ```plaintext
> #! /bin/bash
> 
> echo -n "Server no.: " ; read server
> echo -n "Remote file: " ; read remote
> echo -n "Local file: " ; read local
> 
> echo "SCP:> $server $remote $local"
> 
> exit 0
> 
> ```
> 
> ```plaintext
> [house@leonov] sh test.bash
> Server no.: 12
> Remote file: this
> Local file: that
> SCP:> 12 this that
> 
> ```

Ok, but this does not expand the alias for the servername.

---

<div class="post-metadata">

### Author: ![peterro](https://community.unix.com/letter_avatar/peterro/32/5_5575768a8748004e209b776fc1b2916d.png) [@peterro](https://community.unix.com/u/peterro)
#### Post date: [September 29, 2009, 6:31pm UTC](https://community.unix.com/t/expanding-alias-from-a-variable/250267/5 "2009-09-29T18:31:08Z")

</div>

Where do the aliases get defined?

---

<div class="post-metadata">

### Author: ![locutus01](https://community.unix.com/letter_avatar/locutus01/32/5_5575768a8748004e209b776fc1b2916d.png) [@locutus01](https://community.unix.com/u/locutus01)
#### Post date: [September 29, 2009, 6:56pm UTC](https://community.unix.com/t/expanding-alias-from-a-variable/250267/6 "2009-09-29T18:56:05Z")

</div>

> [@peterro](#):
>
> Where do the aliases get defined?

~/.bashrc

Works on the console.

---

<div class="post-metadata">

### Author: ![peterro](https://community.unix.com/letter_avatar/peterro/32/5_5575768a8748004e209b776fc1b2916d.png) [@peterro](https://community.unix.com/u/peterro)
#### Post date: [September 29, 2009, 7:38pm UTC](https://community.unix.com/t/expanding-alias-from-a-variable/250267/7 "2009-09-29T19:38:42Z")

</div>

That wouldn't be part of your script environment though. Are you using these aliases for something else or mainly this? You could source your .bashrc or keep the definitions in a separate file and source that whenever you have a script that needs to access the definitions.

---

<div class="post-metadata">

### Author: ![dr.house](https://community.unix.com/letter_avatar/dr.house/32/5_5575768a8748004e209b776fc1b2916d.png) [@dr.house](https://community.unix.com/u/dr.house)
#### Post date: [September 30, 2009, 2:36am UTC](https://community.unix.com/t/expanding-alias-from-a-variable/250267/8 "2009-09-30T02:36:05Z")

</div>

> [@peterro](#):
>
> You could source your .bashrc or keep the definitions in a separate file and source that whenever you have a script that needs to access the definitions.

Even though this could work ([reference](http://linux.derkeiler.com/Mailing-Lists/Debian/2008-01/msg02570.html)), I'd rather try to keep things separate (- a basic setup I'm using for larger projects: script.main for the main part, script.code for functions and script.data for variables).

--

Further details: [ABS on Aliases](http://tldp.org/LDP/abs/html/aliases.html)
