Help stripping path information from a variable.

Help stripping path information from a variable.

Post by Nikita Synytsky » Sun, 21 Dec 2003 04:34:46



Hello all!

This should be really simple, but for some reason the solution evades me.  
Consider this:

#!/bin/bash
my_var = '/home/nikita/foo.txt'
echo $my_var

This will print

/home/nikita/foo.txt, of course.  I want it to print just "foo.txt"--I want
to strip out the pathname info.  How do I do it in BASH?  

TIA.

Nikita.

 
 
 

Help stripping path information from a variable.

Post by Ed Morto » Sun, 21 Dec 2003 04:46:29



> Hello all!

> This should be really simple, but for some reason the solution evades me.  
> Consider this:

> #!/bin/bash
> my_var = '/home/nikita/foo.txt'
> echo $my_var

> This will print

> /home/nikita/foo.txt, of course.  I want it to print just "foo.txt"--I want
> to strip out the pathname info.  How do I do it in BASH?  

my_var='/home/nikita/foo.txt'
echo ${my_var##*/}

        Ed.

 
 
 

Help stripping path information from a variable.

Post by Chris F.A. Johnso » Sun, 21 Dec 2003 04:53:34



> Hello all!

> This should be really simple, but for some reason the solution evades me.  
> Consider this:

> #!/bin/bash
> my_var = '/home/nikita/foo.txt'
> echo $my_var

> This will print

> /home/nikita/foo.txt, of course.

  No, you'll get an error message:

bash: my_var: command not found

Quote:>  I want it to print just "foo.txt"--I want
> to strip out the pathname info.  How do I do it in BASH?  

my_var=/home/nikita/foo.txt
echo ${my_var##*/}  

--
    Chris F.A. Johnson                        http://cfaj.freeshell.org
    ===================================================================
    My code (if any) in this post is copyright 2003, Chris F.A. Johnson
    and may be copied under the terms of the GNU General Public License

 
 
 

Help stripping path information from a variable.

Post by Charles Dem » Sun, 21 Dec 2003 06:40:31




Quote:>Hello all!

>This should be really simple, but for some reason the solution evades me.  
>Consider this:

>#!/bin/bash
>my_var = '/home/nikita/foo.txt'
>echo $my_var

>This will print

>/home/nikita/foo.txt, of course.  I want it to print just "foo.txt"--I want
>to strip out the pathname info.  How do I do it in BASH?  

>TIA.

>Nikita.

man basename

or

echo $my_var sed 's:.*/::'

Chuck Demas

--
  Eat Healthy        |   _ _   | Nothing would be done at all,

  Die Anyway         |    v    | That no one could find fault with it.

 
 
 

Help stripping path information from a variable.

Post by Nikita Synytsky » Sun, 21 Dec 2003 14:21:20



Quote:>> #!/bin/bash
>> my_var = '/home/nikita/foo.txt'
>> echo $my_var

>> This will print

>> /home/nikita/foo.txt, of course.
>   No, you'll get an error message:

> bash: my_var: command not found

Of course it will.  Spaces are my downfall--a carry-over habit from C++.

Quote:>>  I want it to print just "foo.txt"--I want
>> to strip out the pathname info.  How do I do it in BASH?  

> my_var=/home/nikita/foo.txt
> echo ${my_var##*/}  

Thanks a lot.  I knew the solution was a few characters long, but just
couldn't figure it out.

Nikita.

 
 
 

1. Stripping off path names: "find" and variables

Hi.  I've got two related questions:

1.  I would like to use the find command to return a list of files
without the paths attached, i.e., it would just return the file names.

2. Given a variable that contains a list of path/file names, I'd like
to be able to save the string in another variable but with the paths
stripped off.

Thanks for any input!

Ken

2. ntp always resets clock instead of slewing clock

3. Help: Shell variable content stripping !

4. Auto power off for linux: Can't get it to work on 2.4.5 and 2.4.10 kernels

5. how to add a path to my path variable

6. ARP For alias

7. Function to add path element to path variable

8. newbie scripting question

9. how to get symbol information of a stripped object file

10. How to get symbol information of a stripped object file

11. Stripping domain information from reply-to address

12. some help needed with path variable

13. Need help editing PATH Variable