17

I'd like to know how can I get zsh to behave like bash regarding this: I want command outputs to always show up right below these commands in terminal.

For example, when I run the following command its results show up like this in bash:

john-doe@machine:~$ aws sts get-caller-identity
{
    "UserId": "USERID",
    "Account": "123456789",
    "Arn": "arn:aws:iam::123456789:user/jonh-doe"
}

Whereas when executed in zsh, the results show up in a separate "editor" which I can quit upon seeing these results, but once I quit I also lose access to the command's output. There's a screenshot below:

command output is lost once "editor" is quit (command execution was succesful)

I don't want to always have to pipe outputs to a file.

I'm running Ubuntu 20.04 and oh-my-zsh with theme powerlevel10k (I tested other themes as well and zsh behaviour remains the same as I mentioned above). I also tested using Terminator and Ubuntu's default Terminal program.

Is there any setting to override this zsh behaviour?

Any help will be appreciated. Thank you!

2
  • Have you tried without oh-my-zsh? The simplest aproach would be to run zsh -f which virtually sources no config files. However, my guess is, that you set some environment variables inside your ~/.bashrc which you don't have in your ~/.zshrc (or vice versa) and these environment variables influence the behavior of the (to me unknown) aws command. Typical variables would be PAGER, LESS, VISUAL...
    – mpy
    Jan 10, 2022 at 20:27
  • Thank you @mpy You are right about oh-my-zsh: the command I exemplified works just like bash when oh-my-zsh is disabled. II'll inspect these rc files and try to find variables having similar names to the ones you've mentioned. Jan 11, 2022 at 2:24

2 Answers 2

23

The behavior you're seeing has nothing to do with zsh vs bash. The behavior occurring because of how oh-my-zsh modifies the zsh shell environment. In ~/.oh-my-zsh/lib/misc.zsh, you'll find these two lines:

env_default 'PAGER' 'less'
env_default 'LESS' '-R'

What env_default does is set an environment variable's value only if it hasn't already been set, so as its name implies, it defines the default value for a variable. It also exports the variable when it sets it. So if the variables PAGER and LESS have not yet been set to anything else, these lines are the equivalent of:

export PAGER='less'
export LESS='-R'

What these lines are doing is setting the default Linux pager to less. This is what leads to the behavior you see that is annoying you. If you run these two lines in a bash shell, it will start acting in this same way.

There are a number of ways to disable this behavior by setting the value of one of these two variables explicitly before the code gets to running these two lines of code. A good place to do that is in ~/.oh-my-zsh/custom/config.zsh. One way is to set the PAGER env var to an empty string, like this:

export PAGER=""

Another thing you can do is configure the less tool to not exhibit this particular behavior unless the output in question is at least a full console window worth of output. This would prevent the behavior in the case of your example. You can do that with this line:

export LESS="-F -X -R"

The -F option is what does this. I don't know why the -X option is needed as well, but it seems to be.

Finally, I'll point out that each particular Linux tool decides if it should use a pager or not. If it does, it usually honors the PAGER variable when doing so. The ls command, for example, doesn't use a pager at all. No matter how much output it prints to the console, it won't exhibit paging behavior. If a command uses a pager, you can often disable this behavior just for that tool with a tool-specific command. For example, to disable the pager just for git, you can do this:

 export GIT_PAGER=""

You can also disable the pager for a single git command. For example:

git --no-pager branch

will run git branch without running the output through the current pager.

I don't know if there's any way to configure if/how just the aws command utilizes a pager. The other answer gives some options as to how to turn off paging for a single arbitrary command. Here's another way, which disables the pager per the information given above:

PAGER="" aws sts get-caller-identity
4
  • Thank you! I had this issue with the GitHub CLI and all of my gh api commands returning to a different screen. Adding export PAGER="" to my .zshrc fixed this. Aug 29, 2022 at 17:15
  • 1
    awesome. I'm always glad to help with stuff like this, having been on the other end of such issues many times in the past. What DID we do before SO, lol?
    – CryptoFool
    Aug 30, 2022 at 7:31
  • @CryptoFool turns out it's also possible to disable AWS CLI's pager behaviour by exporting AWS_PAGER="". I added this line to my .zshrc file it worked like a charm. Your answer was really helpful for me to understand zsh settings and the overall pager behaviour. Thank you!! Nov 5, 2022 at 14:34
  • 2
    I really appreciate answers like these that cover both the 'how' and the 'why'. for me personally, in order to invoke the pager only when contents overrun the window, I had to drop the '-X' and use 'export LESS="-F -R"' ref: unix.stackexchange.com/a/329811 Apr 24, 2023 at 16:23
0

the quick fix is to just run it with bash.

bash -c "aws sts get-caller-identity"

or as said by mpy, you should check your config file or use one of this command

aws sts get-caller-identity | cat
aws sts get-caller-identity | less
1
  • Thanks @MJBN. I tried all 3 suggestions and piping to cat temporarily solves my issue. Invoking the command from bash or piping to less both caused the same behaviour I mentioned in my question. Jan 11, 2022 at 2:34

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .