0

I am receiving an input from the user which looks like follows:

echo +++Your input:+++
read USER_INPUT

The way I should use it is to retrieve the full name of a folder which starts with that input, but that contains other stuffs right after. All I know is that the folder is unique.

For example:

User input

123456

Target folder

/somepath/someotherpath/123456-111-222

What I need

MYNEED=123456-111-222

I was thinking to retrieve this with an MYNEED=$(ls /somepath/someotherpath/$USER_INPUT*), but if I do this I will get instead all the content of /somepath/someotherpath/123456-111-222 because that's the only folder existing with that name so the ls command directly goes to the next step.

May I have your idea to retrieve the value 123456-111-222 into a variable that I will need to use after?

Matteo NNZ
  • 11,930
  • 12
  • 52
  • 89
  • If you really need the name of the directory, you can do the full directory listing and search for it, if it is unique. But if all you want to do is to cd into that directory, you can simply type: – Thomas Hedden Jan 13 '17 at 16:56
  • I'm sorry, I left my edit too long and I couldn't save it. Here is the rest of what I was writing: $ cd 123456* If there is only one directory beginning with "123456", the cd command will figure out which directory to go into. – Thomas Hedden Jan 13 '17 at 17:04
  • Possible duplicate of [Extract filename and extension in Bash](http://stackoverflow.com/questions/965053/extract-filename-and-extension-in-bash) – hansaplast Jan 13 '17 at 20:01

1 Answers1

0

basename extracts the filename from the whole path so this will do it:

MYNEED=$(basename /somepath/someotherpath/123456*)
hansaplast
  • 11,007
  • 2
  • 61
  • 75