Pressing Enter (bash)

Hey, I'm writing this BASH script, I recently started learning BASH after I did Java and I'm pretty new to the syntax.

Anways, what I want to do is simple, I coudn't find the right information though:

Let's say I make a :

read -p "Press ENTER to go back to menu" choice
.....

What is the command that would let me perform this? I need to way for the User to press ENTER, not a silly Y/N. (It's for general accomplishment actually haha).

-Yakuzan

P.S : Example much appreciated!

Try this:
echo -n "Press Enter: "; read MyVar; echo $MyVar

$MyVar will be whatever you type, but just hitting enter will print nothing

Your code will work as is.

The script will wait until the user presses ENTER. You might add an -s option to prevent any other characters being echoed to the screen, and you don't need a variable name since you aren't going to check it:

read -sp "Press ENTER to go back to menu"
echo "Thank you!"

Wasn't sure if Yakuzan needed to take in a Menu Choice or not...

I was assuming that was the point. Take in choice if given, else just do nothing.

You are correct though either way Yakuzan's original code will work as is and will even store the response in choice if needed.

Il use this thread to ask most of my questions since my Linux book blows. It only covers basic BASH since it's not a BASH guide.

And since my openSUSE doesn't have internet on it I'm stuck going from one system to another.

Btw, to understand my concept:

1- Show date
2- Move a folder
3- See current location

That is the menu which is a script called ./menu
If the script reads 1 then you get shoot to ./date
Then I ask to press enter to show back the menu(Thanks a lot :D)
Then back to ./menu

Excetera, each number has it's own script.

As to todays question:

Is there a way to force the user to press enter and nothing else? Does it require an if statement? I saw you mention -s, what exactly do you mean?

P.S Huge thanks to you guys, I'm spending a few hours a day on BASH, Il get it sorted out in no time!

The -p flag is prompt (basically use the following text as the prompt)
The -s flag is silent mode (do not echo characters to the screen)

As to forcing the user to press "Enter" only... why?
No matter what they press it will either be passed to a variable ($choice in you example), or completely discarded as in cfajohnson's example.

Either way Enter was pressed and if you do not use $choice (in you example) the rest doesn't matter.

How can I write a command right after an echo. It`s not working:

echo "Date : " `date + %d`

Am I writing it wrong?

Use printf:

printf "Date : "
date +%d

I've never heard of printf, thanks a lot. Il read the man associated with it.

Edit: I just found out that you can also write echo "Date : " $(date +%d-%m)

also this works as well:

echo "Date : `date +%d`"

Note the backtick command "inside" the double quotes for the echo command and NO space between the "+" and the format string (+%d).

Hey, is their a command that directly displays the current directory?

I'm not talking about pwd.

I want the underlined part of let's say this pwd:

$HOME/bin/workfile1

See, I don't want the whole path, just the last part.

I though I could maybe pipe pwd with a few options but it's not going too great on my side.

echo "${PWD##*/}"

also:

basename `pwd`

or

basename ${PWD}

Many ways to skin a cat in *nix

Two exernal commands where none is needed.

An exernal command where none is needed.

But some are far more efficient than others.

oops...

I guess we are assuming echo is built-in and not /bin/echo (GNU external).

[user@host ~]$ echo --version
--version
[user@host ~]$ /bin/echo --version
echo (GNU coreutils) 6.10
Copyright (C) 2008 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

It has been built into every shell for probably about 20 years. POSIX requires that such commands also be available as external commands.

printf, which is recommended over echo, is built into all major POSIX shells.

I realize it is built-in and has been for a while. I just get so used to using complete paths when writing scripts that I overlook the built-ins from time to time.

I have seen more than a few scripts fail because the ${PATH} var is not set as expected when running in a cron or run remotely.

A quick way around that is to use full paths for all commands and when you get used to that technique in a script... built-in commands get used far less then their external counterparts.

Another thing that I notice is that the built-ins behave differently than expect in some cases since they are usually not the full replica of their external counterparts.

Take for example my previously posted echo example with the --version flag.

Either way, I agree that more efficient is better and certainly your example was shorter and has less commands than mine.

I was just trying to point out that the parameter expansion constructs are not always the easiest to remember and that their ARE alternatives. Some are far easier to remember as well. Efficient is also from the view point of the person writing the script. How efficient is it when you have to go read man pages or google and answer and you spend 20-30 extra minutes writing a single line because you cannot remember the exact method for the parameter expansion you want to use?

Absolutely. String operations can also be difficult to apply to a given problem when there isn't one to do exactly what you want. In any case, it's useful to have a list of them. Since bash 3, it supports built-in regular expressions too...

I'll have to disagree with you on that one. In lower-level languages like C, the easier way is often better because its clearer and simpler and not that much slower in any case. But the cost of launching a process is enormous, you should use processes for:

  • things you positively cannot do in bash, i.e. network operations
  • things where the process does a lot of work, i.e. tar/gzip

Launching lots of short-lived processes for tiny things can easily magnify the running time of your script to the point where it takes huge amounts of time to do anything non-trivial.

More than "a while": for the entire life of the Bourne shell and its successors.

I have seen more than a few scripts fail because the full path was used. It's far easier to adjust the PATH, if necessary, than to change all the commands when running a script on a different system.

In an efficient script, it is quite the opposite. Many of my scripts call no more than one external command, and many call none.

I haven't seen that.

The use of echo is deprecated, and it should not be used when the contents of its arguments are not known in advance.

20-30 minutes to do: "man bash / Parameter Expansion <ENTER>"???
You must be joking!!

You might want to use my sman function (see below).

Besides, after using them a few times, you will not have to look them up, and your scripts will be much faster. Learn them now before bash4 is released; it has many more parameter expansions (some of which I suggested).

sman() ##display a man page with a search 
{
    LESS="$LESS${2:+ +/$2}" man "$1"
}

Since I'm in computer science I asked my teacher, he showed me the basename method, worked like a charm.

Thanks for the
echo "${PWD##*/}"

Will try understanding what does ##*/ mean.