[SOLVED] Possible to use an alias to print a partial command after the prompt, in order to complete manually?

I’ve been trying a few things which haven’t worked. maybe they do in other distros which is why I ask here.

[user@host] $ echo “test \c”
test \c

[user@host] $ echo -e “test \c”
test [user@host] $ echo -e “test \c”

[user@host] $ printf “test \c”
test \c[user@host] $

I’m trying to create an alias where I can simply paste the rest of the command & then execute.

What do you actually want it to print?

.ech. scratch that.

If you want an alias to form the first part of a command … why not do just that?
ex:
alias myquery='man'
Then you could

myquery alias

But be aware … an alias is for commands … not strings of text.
For that use environment variable.
Like this:

MYAPP="pacman"

then you could

myquery $MYAPP

or

myquery $MYAPP-mirrors

@cscs:Thanks very much for that, I will give it a go a bit later today & see how I get on.

I know how to do this sort of thing with a Commodore 64 in BASIC and assembly but am not quite up to speed with bash! (I had an suto-restart-on-crash trap built into my C=64 GUI program which basically wrote stuff into the keyboard buffer, which might give an indicator as to how rusty I’ve gotten with “code” over the subsequent years).
Cheers :slight_smile:

As they say, “every little helps” and is a part of the great learning experience we have with Linux.

@BG405, I’ve found at my skill level, that it is a pretty tricky thing to get bash to do.

I had the need to be able to do in a script I wrote some years ago, & Joshua, (a big helper to the Manjaro community in times gone by (at least) wrote a little python to make it possible for me.

So I’ll post my script below, which will likely give you something to go on with for a while. :slight_smile: There is a pile of escape codes in it to bring various colours to the script, just over look them, they make it look much more complicated than it really is (also, please forgive the wiki markup in the following, I couldn’t be bothered editing it out of my backup):

=What does mhwd-kern.sh do?=

*’’‘mhwd-kern.sh’’’ is a bash script (with a little important python included - my thanks to Joshua), that attempts to make using all of the ‘’‘mhwd-kernel’’’ functions easier & hopefully more understandable. ‘‘It is particularly suitable to those of us that like to use the terminal when they are listing available or installed kernels; adding/removing or replacing kernels.’’ ‘’‘mhwd-kern.sh’’’ provides an easy to understand menu of choices that are simply chosen by entering the number of the menu command of your choice & then hitting the Enter key to run your chosen options command(s). After the Enter key is hit, follow up instruction is displayed if appropriate.


=Installation=

You can copy the script (supplied below) into either your home directory ~/ or you could put it in /usr/bin . (Or anywhere else for that matter, just make sure that your system is configured in a way that will find it. It is already configured to find the script in ~/ & /usr/bin .)


=Use an alias to call mhwd-kern.sh=

I call it with a little ‘’~/.bashrc alias’’, as seen below. Add the following to your ‘’~/.bashrc’’ so that you can then call the script by typing ‘’‘mk’’’ at the terminal prompt:

#----------------------------------------

mk - calls MHWD-Kernel commands via script/menu:

#-----------------------------------------------

alias mk="~/mhwd-kern.sh"

Change the path to ‘’/var/bin/mhwd-kern.sh’’ if you put the script there. You can of course change the ‘‘mk’’ alias to anything that you prefer, too.


=Here is the script=

Paste it into your favourite text editor & save it to ~/ or /var/bin :

#!/usr/bin/env bash

mhwd-kern.sh written by handy with some python help from Joshua.

6-March-2014: Joshua made his much appreciated python contribution work

great, allowing me to release this little beast into the wild!

5-March-2014: Have improved the effecticiency (I think I coined a new

word! lol) of the script, doing away with the need to cut & paste anything.

Joshua showed me the trick.

##########################################################

4-March-2014: Created mhwd-kern.sh - a simple bash script that creates

a menu with the mhwd-kernel options listed. Once you have made your

choice from the menu’s options, you enter the number [?] of your chosen

option into the terminal & hit Enter, which will take you to the next

step(s) to complete your chosen task.

If you don’t want to make a choice hitting Enter will terminate the script.

###########################################################

err() {
ALL_OFF="\e[1;0m"
BOLD="\e[1;1m"
RED="${BOLD}\e[1;31m"
local mesg=$1; shift
printf “${RED}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n” “$@” >&2
}

msg() {
ALL_OFF="\e[1;0m"
BOLD="\e[1;1m"
GREEN="${BOLD}\e[1;32m"
local mesg=$1; shift
printf “${GREEN}==>${ALL_OFF}${BOLD} ${mesg}${ALL_OFF}\n” “$@” >&2
}

#-----------------------------------------------------

Python shit = python FUNCTION - from Joshua follows:

#-----------------------------------------------------

function input {
python2 -c ‘import sys,readline;readline.set_startup_hook(lambda: readline.insert_text(sys.argv[2]));sys.stderr.write(raw_input(sys.argv[1]))’ “$@” 3>&1 1>&2 2>&3
}

#----------------------------------------------------------------

The above was it, now we wrangle with it below, somewhere…

#----------------------------------------------------------------

The menu:

clear # Clear the screen.

echo
echo -e "\e[1;32m mhwd-kern.sh "
echo
echo -e " \e[0;33m Enter Your chosen Option’s number \e[0;32m[\e[1;37m?\e[0;32m] \e[4;37mOR\e[0m\e[0;32m hit \e[0;31mReturn\e[0;32m to \e[0;31mexit\e[0;32m. "
echo
echo
echo -e " [\e[1;37m1\e[0;32m]\e[0;32m List available Manjaro Kernels: \033[0m mhwd-kernel -l "
echo
echo -e " \e[0;32m [\e[1;37m2\e[0;32m]\e[0;32m List installed Kernels: \033[0m mhwd-kernel -li "
echo
echo -e " \e[0;32m [\e[1;37m3\e[0;32m]\e[0;32m Install new kernel(s): Add a valid linux kernel number "
echo -e " directly after \e[1;37mlinux\e[0;32m \e[0;33m(no space)\e[0;32m to the following command, "
echo -e " where you can also choose to add (as shown in the "
echo -e " following example) multiple kernels (always separated by "
echo -e " a space): \033[0m sudo mhwd-kernel -i \e[1;37mlinux\033[0m linux313 linux312\033[0m "
echo
echo -e " \e[0;32m [\e[1;37m4\e[0;32m] Remove one or more installed kernels: (leave at "
echo -e " least one!). The following example has both the \e[0;33m34\e[0;32m & \e[0;33m311\e[0;32m "
echo -e " kernels included: \033[0m sudo mhwd-kernel -r linux\e[0;33m34\033[0m linux\e[0;33m311 "
echo
echo -e " \e[0;32m [\e[1;37m5\e[0;32m] Remove currently running kernel & install those "
echo -e " specified: \033[0m sudo mhwd-kernel \e[0;33m<add kernel(s) here> \e[0;31mrmc\e[0;32m "
echo -e " \e[0;33mNote: \e[0;32mthe \e[0;31mrmc\e[0;32m option is essential to this command. "
echo
echo -e " \e[0;32m [\e[1;37m6\e[0;32m] Display the mhwd-kernel help output in "
echo -e " the terminal: \033[0m mhwd-kernel -h "
echo
echo -e “\033[1m Enter the Number of Your Choice: \033[0m”
echo

read option

case “$option” in

Note variable is quoted.

“1”)
echo
msg "Currently Available Manjaro Kernels: "
echo
mhwd-kernel -l
echo
#mhwd-kern.sh
;;

Note double semicolon to terminate each option.

“2”)
echo
msg "Kernels currently installed on this machine: "
echo
mhwd-kernel -li
echo
#mhwd-kern.sh
echo
;;

Note double semicolon to terminate each option.

“3”)
echo
msg “Kernel Installation”
echo
echo -e "\e[0;32m Enter the Kernel number directly after \e[1;37mlinux\e[0;32m & hit Enter: \033[0m "
echo
printf “sudo mhwd-kernel -i \e[1;37mlinux\033[0m”
read versionNumber
echo
echo -e "\e[0;32m Input sudo password & hit \e[0;31mEnter\e[0;32m to install following kernel: \033[0m "
echo
printf “sudo mhwd-kernel -i linux$versionNumber \n”
echo
sudo mhwd-kernel -i linux$versionNumber
exit 0
echo
;;

Note double semicolon to terminate each option.

“4”)
echo
msg “Remove one or more kernels:”
echo
echo -e "\e[0;32m To remove one Kernel, enter the number of the kernel "
echo -e " you wish to remove after \e[1;37mlinux\e[0;32m & hit Enter. "
echo -e " Remove multiple kernels, (separate with a space) as follows: "
echo -e " e.g.\033[0m sudo mhwd-kernel -r \e[0;33mlinux34 linux310 \033[0m "
echo
printf “sudo mhwd-kernel -r \e[1;37mlinux\033[0m”
read versionNumber
echo
echo -e "\e[0;32m Input sudo password & hit \e[0;31mEnter\e[0;32m to \e[0;31mRemove\e[0;32m the following kernel(s): \033[0m "
echo
printf “sudo mhwd-kernel -r linux$versionNumber \n”
echo
sudo mhwd-kernel -r linux$versionNumber
exit 0
echo
;;

Note double semicolon to terminate each option.

“5”)
echo
msg “Remove currently running kernel & install one or more new kernels:”
echo
echo -e "\e[0;32m This command \e[0;31mRemoves\e[0;32m the kernel you booted with. "
echo -e " It is also used to \e[0;33mInstall\e[0;32m one or more kernels by adding "
echo -e " them to the command below. "
echo -e " You add multiple kernels, (separated by a space) as follows: "
echo -e " e.g.\033[0m sudo mhwd-kernel -i \e[0;33mlinux34 linux310 rmc\033[0m "
echo
versionNumber=$( input 'sudo mhwd-kernel -i ’ ‘linux rmc’)
echo
echo -e "\e[0;32m Input your sudo password & hit \e[0;31mEnter\e[0;32m to \e[0;31mRemove\e[0;32m the current "
echo -e " kernel, & to \e[0;33mInstall\e[0;32m the kernel(s) you just chose: \033[0m "
echo
printf “sudo mhwd-kernel -i $versionNumber\n”
echo
sudo mhwd-kernel -i $versionNumber
exit 0
echo
;;

Note double semicolon to terminate each option.

“6”)
echo
msg "Following is the output of the mhwd-kernel -h command: "
echo
mhwd-kernel -h
echo
;;

Note double semicolon to terminate each option.

esac

exit 0


=mhwd-kern.sh MUST be made executable=

So that the script will run you need to set the executable bit for your user account.

In the terminal, change directory to the directory where you stored the file (I used ‘’’/usr/bin’’’ in the following example) & then run the chmod command as follows:

$ cd /usr/bin
$ chmod u+x mhwd-kern.sh

I don’t really understand the problem here - you paste with ctrl+shift v.

Try something like this:
edit your .bashrc or .zshrc
add a line:
alias install='pamac install'

Now copy this line:

pamac sl

Then open a fresh terminal and type ‘install’ followed by ‘ctrl+Shift+V’.

So your alias ‘install’ followed by a pasted command works or not? It should try to use pamac to install both pamac and sl… it should only think about installing sl if you don’t already have it.

Next, run ‘sl’ and laugh.

I’ll try to add my first picture (a screenshot) to this forum - if they host it on their server then I’ll succeed, if not then I won’t, as I won’t sign up to some hosting service:

Following is screenshot of the above posted script’s menu. It should show you that it has the ability of allowing you to input text on a command line & then execute it.

NOTE: Sorry, it won’t work, you can no longer post pictures here it would seem.

[edit:] OK, I just checked & the wiki page I wrote re. that script still exists, so if you have a look there you will find out what you can do by incorporating the tiny bit of python that it uses, so you can make aliases that allow you to be presented with active bash command lines in the terminal, where you just add whatever to them & hit enter to run them.

I suppose I really should dig back into the script & make use of the facility for myself, & then make a how-to on it.

Its just that I’ve got old & lazy(er)… :wink:

[edit-2:] 0ops, I forgot to put the wiki page URL in (add the “h” at the start so the link that I’m not allowed to post works):

ttps://wiki.manjaro.org/index.php?title=Mhwd-kern.sh

NOTE 2: I think I understand now that I haven’t made enough posts or something to be allowed to post images or add links… :frowning:

1 Like

@BG405, OK, after having to make my brain think in a way that it hasn’t been doing for quite some time, I have pulled out the essentials (as far as my understanding of your OP is concerned) of that script that I posted previously in this thread & made a very much simplified script that is hopefully well commented & shows an example of putting a relatively complex command line into the terminal that has ability provided for you to arrow key your cursor back in the command to a specific point where you can then add text, hit enter & run the command.

Phew, that was a mouthful. :slight_smile:

The script that I call hb.live.sh (I still use the .sh as it makes it easier to quickly see if something is a bash script). I’ve also put some escape sequences in there for some added colour on/off in places, \e[0;32m it should not be too hard to work out :

#!/usr/bin/env bash
 #
 # 31-8-20: Following is an example of how to use the python function below as a
 # way to be able to edit a bash command string in the terminal. Which
 # is something I could never manage myself using bash.
 # Enjoy - handy :)
 ###########################################################

 #-----------------------------------------------------
 # Python stuff = python FUNCTION - from Joshua follows:
 #-----------------------------------------------------

 function input {
    python2 -c 'import sys,readline;readline.set_startup_hook(lambda: readline.insert_text(sys.argv[2]));sys.stderr.write(raw_input(sys.argv[1]))' "$@" 3>&1 1>&2 2>&3
 }

 #-----------------------------------------------------
 # a good reason to use the above function, as this kind of command is too hard
 # to remember, & I never could find a way for bash to let me edit such
 # when used in an alias. So I'd echo it then have to copy/past & edit it.
 #-----------------------------------------------------

  echo
  echo -e "\e[0;32m    A Python function allows you to edit this command line. "
  echo -e "    Add the name of your video with \e[0;31mNO\e[0;32m space before \e[0;33m.mp4 \033[0m "
  echo -e "\e[0;32m    Use key combo - 'Ctrl C' to exit script without running it. \033[0m "
  echo
  # The following line can be called whatever you want - meaning titleName=$
  # can be named appropriately, you MUST keep the =$() though.
  # Then look at the use of the apostrophies, they are critical to making
  # this work. Notice the space in them just prior to the .mp4 . That is
  # why when you go to edit this line you can't go any further to the left.

  titleName=$( input 'HandBrakeCLI -t 0 -i /media/dvd -o ' '.mp4 -e x264 -b 1000 -B 192 -s 1 --subtitle-burn')

  echo
  HandBrakeCLI -t 0 -i /media/dvd -o .mp4 -e x264 -b 1000 -B 192 -s 1 --subtitle-burn
  exit 0

To run the above script (as with any) you have to make it executable, if you don’t know how to do that, my initial script in this thread tells you down near the bottom of it.

Whether you have HandBrakeCLI installed or not, you can still run this script & edit the command line. I made & tested the script without ever processing a DVD with HandBrakeCLI. :slight_smile:

I’ll now go & make it run from an alias & then come & make another post.

1 Like

You could add a function to your ~/.bashrc but I think when they start to get too big it is easier & tidier to make scripts & stick where it suits you. You can put that location on your system’s default search path if you want.

Anyway, I put my little script in my ~/scripts directory & I made a little alias that calls it like so:

# The next bit is a vast improvement on the old way (found under it), as it
# uses Joshua's python gift to me for the old mhwd-kern script, which
# allows a user to fairly easily (once you've worked out how) put a complex
# (long or short) command on the terminal (from a script/function) & move 
# your cursor around in the command to add data or modify the command. :)

alias hb="$HOME/scripts/hb.live.sh"

Here’s a screen shot (forget the system info at the top):

1 Like

@handy This is really appreciated! I’m (as you can tell) a complete novice when it comes to BASH & Python scripting despite doing practically all system admin via CLI (& SSH). This helps me to get started; will try it out later tonight.

Perhaps I should have said what my original intention was: to have youtube-dl with my preferred parameters & then just paste the URL at the end then press enter. Hopefully this will do the job. I do have full bash completion enabled on this machine by the way.

Then all you need is an alias (maybe put in .bashrc).

alias gimme='youtube-dl -options 1 2 3'

Then you do

gimme URL.com/87696
1 Like

@BG405, If you had of said that to start with more than one of us would have presented you with our particular solutions: :wink:

#!/bin/bash
#
# I run this from ~/.bashrc with the following alias:
# alias dv="sh ./dl-youtube"
# youtube-dl is set in its ~/.config to download to a specific path.

# 136 = 720p (much smaller file size than 1080p).
# 137 = 1080p ~twice the size as that from using "136"
# 248 = 1080p webm format, much larger than if using "137" but best quality available.
# 140 = best sound quality format available.

echo Please, Enter a URL
read URL
/usr/bin/youtube-dl -f 136+140 --prefer-free-formats $URL

## USE THE FOLLOWING WHEN YOU GET THIS ERROR:
## "ERROR: requested format not available"
## & the youtube-dl will use it instead of failing.

/usr/bin/youtube-dl $URL
1 Like

Thanks for this; the simplest solutions are often the best and this works perfectly.

The other really helpful posts here will certainly add to my knowledge and I am bookmarking this thread.

Many thanks all! :slight_smile:

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.