Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oHD3I-0007Gz-Ft for pgsql-general@arkaria.postgresql.org; Thu, 28 Jul 2022 23:36:24 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oHD3G-0007aO-Cg for pgsql-general@arkaria.postgresql.org; Thu, 28 Jul 2022 23:36:22 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oHD3E-0007aD-NB for pgsql-general@lists.postgresql.org; Thu, 28 Jul 2022 23:36:22 +0000 Received: from wout4-smtp.messagingengine.com ([64.147.123.20]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1oHD3B-0000SI-Ka for pgsql-general@lists.postgresql.org; Thu, 28 Jul 2022 23:36:19 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.west.internal (Postfix) with ESMTP id 9246C320034E; Thu, 28 Jul 2022 19:36:15 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Thu, 28 Jul 2022 19:36:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm2; t=1659051375; x= 1659137775; bh=OxeblzsWEsr49xD1PC0BqNSV/MaePzirgovq5JWTlPc=; b=r kcqy4VACEmfOG2nAqKD/rDIE+oEM62Ka2U9Q9cTV7ISw3v3wPICjxoZG0hEeute2 DrbovWyGgyRWiK9wjSNiUcU6FKhBAboTAwf/m1afleFeqwQFfjxQ0xDGGVTmzAKG DdBIJy8VPtVVM79cUhswZhAHsTfhHETNyXQrytoYBkKojFwMhzqxyubcZROdi7Qs vvHz8q3QtsG8+l11Me47YWUlTbfNF8tcg18tMZeMBMvzjY0qoNbBZIN6qW1nK4tw /37SrgNC+c68z/+WnoWhlSCGLAr8fqkV+OJkQ+PjzGrp6vTlgU436MjLbnF4Vniu O3dFTpIcW8Ugb7UcioCmw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-transfer-encoding:content-type :date:date:feedback-id:feedback-id:from:from:in-reply-to :in-reply-to:message-id:mime-version:references:reply-to:sender :subject:subject:to:to:x-me-proxy:x-me-proxy:x-me-sender :x-me-sender:x-sasl-enc; s=fm3; t=1659051375; x=1659137775; bh=O xeblzsWEsr49xD1PC0BqNSV/MaePzirgovq5JWTlPc=; b=wFTDSoN39E98QFSoN jBPSgWOEIxIehHLIh40nhUNWNwyEJ6tEuWOjnZyqMAv8Y//y/4CXopmQ59r+aysX zslUQ6PkKOhBYTImfovS7V9v6buJtmplvdj4Glkp1rQL56oZwPPI776rUAllvG3c A2ogswTykXabJRpa+KwIGp8hg9hn++4+DJUhR3Wg5+bhFx4vWZ5c0sLWmx5AiGyb zbRSLZ63m4VDJKzPmQptFNjfoK7+rCJ5xWkxiI9tCAruEdncqTiNfoIXOZQ0HRCE yxdTYvrsgE9SKoJCuTg2KDLKHxOlqDFfv8nkKw22vzz3tqKqQDh8ntsl0I/CpUjl EPmCg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvfedrvdduhedguddvjecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecunecujfgurhepkfffgggfuffvfhfhjggtgfesth ekredttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdr khhlrghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeelkeegje fhkeetheefudevteehiedvudehkeduleetfedvffeuhedtvdegtdeffeenucffohhmrghi nhepphhoshhtghhrvghsqhhlrdhorhhgnecuvehluhhsthgvrhfuihiivgeptdenucfrrg hrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdr tghomh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 28 Jul 2022 19:36:14 -0400 (EDT) Message-ID: <4299f8dc-7d37-b88c-de12-ac6830d3e10a@aklaver.com> Date: Thu, 28 Jul 2022 16:36:13 -0700 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:91.0) Gecko/20100101 Thunderbird/91.11.0 Subject: Re: Syntax error when combining --set and --command has me stumped Content-Language: en-US To: Ron , pgsql-general@lists.postgresql.org References: <352ceea1-029d-726e-89b5-74be4946e67b@gmail.com> From: Adrian Klaver In-Reply-To: <352ceea1-029d-726e-89b5-74be4946e67b@gmail.com> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 7/28/22 12:40, Ron wrote: > > $ alias psql12 > alias psql12='/usr/lib/postgresql/12/bin/psql -p5433' > > This works ask expected: > > $ psql12 --set num=42 -ac "\echo :num" > echo :num > 42 > > And so does this: > > $ psql12 --set num=42 > psql (12.11 (Ubuntu 12.11-1.pgdg18.04+1)) > Type "help" for help. > > postgres=# select :num; >  ?column? > ---------- >        42 > (1 row) > > > But trying to use a variable (both with and without single quotes) in a > --command statement other than "\echo" throws a syntax error at the colon: > > $ psql12 --set num=42 -ac "select :num;" > select :num; > ERROR:  syntax error at or near ":" > LINE 1: select :num; >                ^ > $ psql12 --set num=42 -ac "select :'num';" > select :'num'; > ERROR:  syntax error at or near ":" > LINE 1: select :'num'; >                ^ > > What secret sauce am I missing to get this to work? From here: https://www.postgresql.org/docs/current/app-psql.html -c command ... Because of this behavior, putting more than one SQL command in a single -c string often has unexpected results. It's better to use repeated -c commands or feed multiple commands to psql's standard input, either using echo as illustrated above, or via a shell here-document, for example: psql < \set num 42 > SELECT :num; > EOF Null display is "NULL". ?column? ---------- 42 (1 row) -- Adrian Klaver adrian.klaver@aklaver.com