1

The echo command outputs differing results based on the expression passed to it. The working directory is /home/etc

$ echo .*/
/ ../ .cache/ .config/ .gnupg/ .local/ .mozilla/ .ssh/

$ echo ./*
./Desktop ./Documents ./Downloads ./Music ./Pictures ./Public ./snap ./Templates ./Videos

$ echo .*/*
.cache/event-sound-cache.tdb.d410907cf15246578458d0ad7919eb5e.x86_64-pc-linux-gnu .cache/evolution .cache/fontconfig .cache/gnome-screenshot .cache/gnome-software .cache/gstreamer-1.0 .cache/ibus .cache/ibus-table .cache/libgweather .cache/mesa_shader_cache .cache/mozilla .cache/thumbnails .cache/ubuntu-report .cache/update-manager-core .cache/wallpaper .config/dconf .config/enchant .config/eog .config/evolution .config/gedit .config/gnome-initial-setup-done .config/gnome-session .config/goa-1.0 .config/gtk-3.0 .config/ibus .config/nautilus .config/pulse .config/rclone .config/update-notifier .config/user-dirs.dirs .config/user-dirs.locale ./Desktop ./Documents ./Downloads .gnupg/private-keys-v1.d .gnupg/pubring.kbx .gnupg/trustdb.gpg .local/share .mozilla/extensions .mozilla/firefox .mozilla/systemextensionsdev ./Music ../ec ./Pictures ./Public ./snap ./Templates ./Videos

$ echo */*/
Downloads/sync/ Downloads/testdir/ snap/gnome-calculator/

The aim is to reduce the number of commands to get the output. Can there be a single echo statement that combines the output of echo .*/ and echo ./* other than echo .*/ */?

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
Ryan
  • 177
  • 2
    I do not see a question here, but it seems like you would want to read up on shell globbing to understand why you are getting the output that you are. The shell uses the asterisks ("*") as a wildcard, your echos are just displaying files and directories in your local directory and the difference is that some start with a period character (".") while some do not. – GracefulRestart Jan 13 '20 at 17:33
  • 2
  • @GracefulRestart - Sorry. I thought I had ended it with a question. I have updated the post with a question. I understand the output. – Ryan Jan 14 '20 at 07:36
  • @steeldriver - I have updated the question and you'll notice that it's different to the question you had referred to a potentially related. – Ryan Jan 14 '20 at 07:38
  • 1
    Assuming the answer was "yes", may I ask what you're wanting to achieve here? – Chris Davies Jan 14 '20 at 09:22
  • @roaima - The aim is to reduce the number of commands to get the output. For instance, rather than repeating echo .*/ ./*, is there a better option? If this is considered to be the most effective solution, I'll accept it. – Ryan Jan 14 '20 at 17:41
  • If none of the answers below are what you are looking for, then you need to make the question clear (as it stands I see some good answers). Be clear about what you are trying to do (goals, then method (how). – ctrl-alt-delor Jan 14 '20 at 18:35
  • @ctrl-alt-delor - It would be helpful if the community can provide guidance on the downvote since the objective is to reduce the command of commands and hence the question can echo be combined to produce a single result of ./ and /? Should the question be re-titled to How can the number of arguments for echo be reduced to return the same output as ./ and /? – Ryan Jan 16 '20 at 06:35
  • Some people incorrectly mark down a question, because it shows lack of knowledge. This is wrong, because that is what questions are for. You do not seemed to have jumped to an (incorrect) solution. So I see no need for the down-vote. The only thing I would like to see added, is why: both because I am curious, and because it may shed light on what the best answer would be. – ctrl-alt-delor Jan 16 '20 at 18:44

3 Answers3

5

Assuming that you know about dot-files (files that start with a dot) and that file-expansion (list files a.k.a globbing) doesn't match those by default.

You can get one list if you do:

$ echo .*/ ./* .*/* */*/

If that is what you meant to ask.


The question now has changed to

Can there be a single echo statement that combines the output of echo .*/ and echo ./* other than echo .*/ */?

Assuming that you mean to list files * and dot-files .* in the pwd:
The (portable) answer is yes:

find * -maxdepth 1 -type d

Limited to bash:

shopt -s dotglob
echo */
  • Sorry. I have updated the question now. Yes. I do know about dot files and file expansion although I am unsure if there is another method other than echo .*/ */? – Ryan Jan 14 '20 at 07:39
  • 1
    @roaima Yes, that's correct, thanks. –  Jan 16 '20 at 10:17
1

It is the shell that does it. Not echo.

This may be more like what you are trying to do. ( shopt -s dotglob; echo * ) It lists all files, but not . and ...

It works in bash.

  • @ctrl-atl-delor - I have updated the question. Yes, I am aware that it is the shell that processes it and not echo. – Ryan Jan 14 '20 at 07:40
0

The echo command outputs differing results based on the expression passed to it.

This is a fundamental misconception. The echo command doesn't see any expression; it simply outputs the set of string literals that it receives as arguments.

The shell is responsible for evaluating the commend line and then executing the first word as a command, with all subsequent words passed as arguments.

Chris Davies
  • 116,213
  • 16
  • 160
  • 287
  • To clarify, how is an expression different to that of an argument? My understanding is that expressions are string values passed to a command to process and output a result. I have also assumed that the shell acts on these arguments and passes these to echo to display the message. – Ryan Jan 14 '20 at 17:19
  • @Ryan I would tend to work on /.* being an expression and the tuple { .bashrc .profile .logout } being literals. – Chris Davies Jan 15 '20 at 23:06
  • @Literals as in arguments? – Ryan Jan 16 '20 at 06:37
  • 1
    @Ryan yes. In your question's first example, the shell sees one expression, evaluates it, and passes the eight literal words to echo. – Chris Davies Jan 16 '20 at 07:19