24

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 Answers2

33

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
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
MJBN
  • 1